field delimiters / scanning in file I/O

Started by
2 comments, last by paulcoz 23 years ago
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
Advertisement
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!
http://www.gdarchive.net/druidgames/
Whats the deal with all those ridiculously long comment lines?
The source tags don''t like to word wrap I guess .

"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!
http://www.gdarchive.net/druidgames/

This topic is closed to new replies.

Advertisement