Another variation of the theme described in dasblinkenlight is:
for (int i = 0; i < 20; i++)
{
int rc;
int number;
if ((rc = scanf(" this is the time for all good men (all %d of them)", &number)) == 0)
{
char remnant[4096];
if (fgets(remnant, sizeof(remnant), stdin) == 0)
printf("Puzzling β can't happen, but did!\n");
else
{
printf("The input did not match what was expected.\n");
printf("Stopped reading at: [%s]\n", remnant);
}
}
else if (rc == 1)
printf("%d: There are %d men!\n", i + 1, number);
else
{
printf("Got EOF\n");
break;
}
}
Try in a file containing:
this is the time for all good men (all 3 of them)
this is the time for all good men (all 33 men)
this is the
time for
all good men
(all
333 of
them)
this is the time for all good men to come to the aid of the party!
Etc.
Conclusion:
1: There are 3 men!
2: There are 33 men!
The input did not match what was expected.
Stopped reading at: [men)
]
4: There are 333 men!
The input did not match what was expected.
Stopped reading at: [to come to the aid of the party!
]
Got EOF
, , 'men)
' ( "of them)
" ). ( , %n
) . , fgets()
( ), , .
"this is the time for all good men
" , "to come to the aid of the party
" .