Re: can fscanf skip reading data conditionally?



"Robert Gamble" <rgamble99@xxxxxxxxx> writes:
John wrote:
I need to read data from the file like the following with name and
score, but some line may only has name without score:

joe 100
amy 80
may

Here's my code, but it couldn't read the line with "may" because there
is no score. Anyone knows what is the workaround to this problem?

char name[20];
int score;

while (fscanf(ifp, "%s %d", name, &score) == 2)
{
printf("%s %d\n", name, score);
}

int i;
while ((i = fscanf(ifp, "%s %d", name, &score)) >= 1)
{
if (i == 2)
printf("%s %d\n", name, score);
else
printf("%s\n", name);
}

fscanf() skips whitespace, including newlines. If you call fscanf
with a format of "%s %d", it will read a blank delimited word, then
look for a decimal integer; if there isn't one on the current line, it
will continue reading lines until it finds either a decimal integer or
something that definitely isn't one.

That might happen to work, but it's fragile. Also, it won't detect
an error such as:

joe 100
amy 80
may

42

fred 97

(I'm assuming you want each name and optional score to be on a single
line.)

A better solution is to read an entire line at a time (use fgets() if
you can assume a maximum line length) and then apply sscanf() to the
resulting string.

An advantage of sscanf() is that it doesn't consume its input; you can
pass the same string to it again with a different format. Another
advantage is that, if the string contains a single line of input, it
won't try to read additional lines; it treats the end of the string
like end-of-file.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: searching what groups a user belong from AD but errorThe Kerberos subsystem encountered an error
    ... I was just about to write back Joe. ... private string BuildFilterOctetString ... Co-author of "The .NET Developer's Guide to Directory Services ... DirectoryEntry searchRoot = new DirectoryEntry( ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: LogonUser access denied
    ... Joe - really appreciate your efforts on this one, i will try the privilege ... private static extern int LogonUser(String lpszUsername, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: 32-bit programs on Windows x64
    ... tiny individual sequences. ... to compare its input string against every possible ... compare one string against one string, ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... machine has "parallelism" ... to compare its input string against every possible valid ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)