Why isn't the user prompted to enter input for the second while loop, even though the end of file indicator has been cleared?
Note that the following code is being run on Bash on Windows (Windows Sub-System for Linux).
#include <stdio.h>
int main(void)
{
int c;
printf("Reading from stdin\n");
while ((c = getchar()) != EOF) {
putchar(c);
}
printf("End of file reached\n");
printf("End of file indicator %s\n", (feof(stdin)) ? "not cleared" : "cleared");
printf("Clearing end of file indicator\n");
clearerr(stdin);
printf("End of file indicator %s\n", (feof(stdin)) ? "not cleared" : "cleared");
printf("Continue reading from stdin\n");
while ((c = getchar()) != EOF) {
putchar(c);
}
printf("End of file reached, all done\n");
return 0;
}
INPUT:
one
two
Ctrl+D
OUTPUT:
Reading from stdin
one
two
End of file reached
End of file indicator not cleared
Clearing end of file indicator
End of file indicator cleared
Continue reading from stdin
End of file reached, all done