Go Back   FlashFXP Forums > >

Programming Need help with C/C++/Delphi? Ask here and make us all laugh.

Closed Thread
 
Thread Tools Rate Thread Display Modes
Old 08-30-2003, 02:10 AM   #1
w3rd
Junior Member
 
Join Date: Aug 2003
Posts: 12
Question parse file q

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?



thanks in advance, w3rd
w3rd is offline  
Old 08-30-2003, 05:00 AM   #2
FTPServerTools
Senior Member
FlashFXP Beta Tester
ioFTPD Scripter
 
Join Date: Sep 2002
Posts: 543
Default

Code:
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
  }
}
FTPServerTools is offline  
Old 08-30-2003, 06:44 AM   #3
w3rd
Junior Member
 
Join Date: Aug 2003
Posts: 12
Default

thanks, i'll try that when i get home

appreciate the response

w3rd
w3rd is offline  
Old 08-30-2003, 10:17 AM   #4
Mouton
Posse Member
Ultimate Scripter
ioFTPD Administrator
 
Join Date: Dec 2002
Posts: 1,956
Default

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.
Mouton is offline  
Old 08-31-2003, 01:00 AM   #5
w3rd
Junior Member
 
Join Date: Aug 2003
Posts: 12
Default

worked great

seems i was parsing it wrong :/

thanks for the help

w3rd
w3rd is offline  
Old 09-01-2003, 10:51 PM   #6
w3rd
Junior Member
 
Join Date: Aug 2003
Posts: 12
Default

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
w3rd is offline  
Old 09-02-2003, 02:38 AM   #7
FTPServerTools
Senior Member
FlashFXP Beta Tester
ioFTPD Scripter
 
Join Date: Sep 2002
Posts: 543
Default

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.
FTPServerTools is offline  
Old 09-02-2003, 02:41 AM   #8
w3rd
Junior Member
 
Join Date: Aug 2003
Posts: 12
Default

Quote:
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
w3rd is offline  
Closed Thread

Tags
end, file, line, parse, read

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Search For Files On A Remote Server chrome307 General Discussion 2 01-02-2005 12:06 PM
site change ratio suggestion Lunatik Suggestions 3 02-13-2003 09:14 PM


All times are GMT -5. The time now is 08:55 AM.

Parts of this site powered by vBulletin Mods & Addons from DragonByte Technologies Ltd. (Details)