Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


field delimiters / scanning in file I/O

This topic is 6481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone please explain to me how to use field delimiters (like commas and newlines) during file input / output? I need to be able to read characters from a file into a char array until a delimiter is reached (like when reading a filename). Also, I want to read in these delimited values until a newline is reached. I think I can handle the EOF. I want to use fopen, fclose, fscan, fread etc.. - how do you read all of the characters before the next delimiter into a buffer, and also check if the end of a line is reached (and I guess how to start reading at the next line when this occurs)? Also, how do you write a delimiter between fields? Thanks, Paulcoz. Edited by - paulcoz on March 20, 2001 9:05:04 PM

Share this post

Link to post
Share on other sites
Well, the easiest way is to just have a buffer (char array) that you fill until you read that symbol, then stop filling the buffer, and hop over the symbol . You can use strtok if you have the data in a buffer already...

FYI: This does not have _any_ error checking
It assumes that fp is a valid FILE pointer, that buffer points to a large enough chunk of memory to hold the data, and that the FILE pointer is at the beginning of the data chunk to parse out.
You could easily modify it though ;)

void GetStringUntilChar(FILE *fp, char *buffer, char ender) {
while((*buffer=fgetc(fp))!=ender) buffer++;
*buffer = ''\0'';

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Whats the deal with all those ridiculously long comment lines?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!