Jump to content
  • Advertisement
Sign in to follow this  

Problems with fscanf

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

Hi all!

I want to parse a file which has the following format:

foam offset: 10.00, 20.00
foam size: 1250.00, 600.00

melting radius: 1.30

supporters: 10


The code I have so far:

fscanf(stream,"foam offset: %lf, %lf",&FoamOffset[0],&FoamOffset[1]);
fscanf(stream,"foam size: %lf, lf\n",&FoamSize[0],&FoamSize[1]);

fscanf(stream,"melting radius: %lf",&ToolRadius);

fscanf(stream,"supporters: %d",&SupporterCount);

for( i = 0; i < SupporterCount; i++ )

But it only reads the first line (debugged: all the other fcanfs return 0). According to the reference, the function should "read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character". But it seems it doesn't. Before I start to hack I ask it here: what's the problem? (and I want to keep it flexible in the future (so it should not be whitespace-sensitive))

Thanks in advance!

Share this post

Link to post
Share on other sites
Solved it.
I read id line by line, then read parse the lines with sscanf. I can parse simply and the lines can be in arbitrary order, but they must be in a new line in order to get parsed. Otherwise stuff that is in the same line won't be parsed. It's my "own format", i can afford to be so very strict about it...

Share this post

Link to post
Share on other sites

fscanf(stream,"foam size: %lf, lf\n",&FoamSize[0],&FoamSize[1]);

Missing a '%' before that 2nd lf.

EDIT: also, don't ignore return values. They're *really* useful!

Share this post

Link to post
Share on other sites
Yes, I noticed that%, that wasn't the issue. The issue was that I misunderstood the reference.
I use the return values.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!