In C++, when the stream is in an error state (e.g. eofbit being set), we cannot perform either read or write operation on that stream. But if it's in C, what happen when the stream is in an error state? It seems that we can still read or write on that stream. Consider the following code:
runing this program, I got
int main( void )
while ( getchar() != EOF ) ;
if ( feof(stdin) )
/* Surprisingly! Can still read from stdin */
putchar( getchar() );
if ( !feof(stdin) )
printf("Why EOF is unset?");
From the output, we know that stdin is in an error state before the second getchar. But as we can see, getchar was still called successfully. Why? And after that getchar was called, the EOF state for stdin was also unset, why?
1Why EOF is unset?
ps, I complied this code with Dev-C++