Problems with fscanf

Started by
4 comments, last by szecs 13 years, 1 month ago
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

70.00
200.00
300.00
400.00
500.00
600.00
700.00
800.00
900.00
1000.00


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++ )
{
fscanf(stream,"%lf",&SupporterPositions);
}

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!
Advertisement
Oh yes, I misunderstood the reference. It only concerns the format string.
I can't solve this one. All my ideas are horrible hacks.
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...

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!
Yes, I noticed that%, that wasn't the issue. The issue was that I misunderstood the reference.
I use the return values.

This topic is closed to new replies.

Advertisement