Re: can fscanf skip reading data conditionally?
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Wed, 27 Sep 2006 18:46:19 +0200
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);
}
You did not specify what you want to do if you encounter this problem.
One example:
char name[20];
int score;
int ret;
while (EOF != (ret = fscanf(ifp, "%19s %d", name, &score)))
{
if (ret > 0) {
printf("%s", name);
if (ret > 1) {
printf("%d", score);
}
}
putchar('\n');
}
Next time, explain what you want to do, what you have, how the
results of your code do not match what you expected, and give
a compilable minimal example. This helps us help you.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- References:
- can fscanf skip reading data conditionally?
- From: John
- can fscanf skip reading data conditionally?
- Prev by Date: Re: can fscanf skip reading data conditionally?
- Next by Date: Re: Length of Variable Names Affect Compiled Executable?
- Previous by thread: Re: can fscanf skip reading data conditionally?
- Next by thread: eeeeeeee
- Index(es):
Relevant Pages
|