PDA

View Full Version : parse file q


w3rd
08-30-2003, 02:10 AM
hey,

i'm trying to parse a file in c++ using strtok() but for some reason i end up with more then just one line in the read buffer...

(i'm using fopen to open the file and fgets to read the buffer)

i tried parsing out the '\n' '\r' and '\0' but still having problems with unnecessary lines in the result.. i need to seperate each line read from the file in some way. another problem i'm having is that if i do this, and there is no \n char at the end (no new line was added, or enter wasnt hit) i get stuck with it saying there are no lines in the file :/ any suggestions?

:confused:

thanks in advance, w3rd

FTPServerTools
08-30-2003, 05:00 AM
FILE *f;
char buf[128],*s=NULL;
f=fopen("filename","rb");
while (!fgets(f,127,buf)){
s=strtok(buf,"\r\n");
while (s){
s=strtok(NULL,"\r\n");
//got line part starting with s
}
}

w3rd
08-30-2003, 06:44 AM
thanks, i'll try that when i get home :)

appreciate the response

w3rd

Mouton
08-30-2003, 10:17 AM
fgets read only one line of your file. it reads text until it finds a \n

"The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first."

If you appear to get more than that, i suggest a line by line debug. I'm pretty sure fgets can't return multiple lines.

w3rd
08-31-2003, 01:00 AM
worked great :D

seems i was parsing it wrong :/

thanks for the help

w3rd

w3rd
09-01-2003, 10:51 PM
since we're on the subject of parsing, i also came across another problem.. parsing incoming data thru a socket

after doing recv() its stored to a buffer with multiple lines.. do i go about parsing that with strtok? or is there a better way u guys know of ?

thanks in advance,
w3rd

FTPServerTools
09-02-2003, 02:38 AM
Well I always parse by hand. basically using recv you can recv a 0x00 byte but you would want to parse that right?
So hence I always write my own specific parser. Even tho it is quite generic.

w3rd
09-02-2003, 02:41 AM
Originally posted by FTPServerTools
Well I always parse by hand. basically using recv you can recv a 0x00 byte but you would want to parse that right?
So hence I always write my own specific parser. Even tho it is quite generic.

i c :/

could you give an example if not too much trouble?

TIA

w3rd