I am just baffled by basic stream handling in C. Even after an hour of googling and reading on the issue I am none the wiser ( and it is not my first attempt to delve into this).
I am trying to read numbers from input until EOF or non-number is reached and be able to distinguish between those 2. From what I understand this should work, but the feof
and ferror
conditions are never true. Why is that ? And could somebody provide me with a working code snippet along with a dummy friendly in-depth explanation?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("number or EOF:\n");
while(scanf("%d",&number) == 1)
{
printf("read number %d\n",number);
}
if(ferror(stdin))printf("error reading\n");
else if (feof(stdin))printf("eof reached\n");
return 0;
}