Re: can fscanf skip reading data conditionally?
- From: "Robert Gamble" <rgamble99@xxxxxxxxx>
- Date: 27 Sep 2006 09:41:40 -0700
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);
}
Robert Gamble
.
- Follow-Ups:
- Re: can fscanf skip reading data conditionally?
- From: Keith Thompson
- Re: can fscanf skip reading data conditionally?
- References:
- can fscanf skip reading data conditionally?
- From: John
- can fscanf skip reading data conditionally?
- Prev by Date: Re: C99 Versus ANSI.
- Next by Date: Re: can fscanf skip reading data conditionally?
- Previous by thread: Re: can fscanf skip reading data conditionally?
- Next by thread: Re: can fscanf skip reading data conditionally?
- Index(es):
Relevant Pages
|