Archived

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

can fscanf() to do it?

This topic is 5524 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

How can i get a statement from a text file one by one? e.g. abc.txt This is testing StatementA read StatementB read I want to get the first statament(This is testing) and then store in string1, and so on. How can i do this? I know i can use fread() function and then loop it until a newline character read for each statement. However, does it has another faster method? Or....can i use fscanf() to perform it?

Share this post


Link to post
Share on other sites
char str[256];
fgets( str, sizeof( str ), file );

The faster way is to load whole file into memory and parse it there.

Share this post


Link to post
Share on other sites
icic. I perfer the method that get all the data from the file. And then separate it. However, does the library provid a function to get the numbers of the characters in a text file?

Thx a lot of.

Share this post


Link to post
Share on other sites
You may be able to use a combination of fseek() to go to the end of the file, and then ftell() to get the current position in the file. That *might* give you the correct file size in bytes, though if you open the file in text mode then it may be off due to newline conversion.

I haven''t tried any of the above, just speculating.

Share this post


Link to post
Share on other sites
quote:
Original post by Meduzza
char str[256];
fgets( str, sizeof( str ), file );

The faster way is to load whole file into memory and parse it there.


sizeof( str ) will always return 4. The proper way would be to use strlen( str )

Share this post


Link to post
Share on other sites