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 gotCode:#include <stdio.h> int main( void ) { int ch; while ( getchar() != EOF ) ; if ( feof(stdin) ) { printf("Oops...EOF!\n"); } /* Surprisingly! Can still read from stdin */ putchar( getchar() ); if ( !feof(stdin) ) { printf("Why EOF is unset?"); } return 0; }
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?Code:745a2344faz ^Z Oops...EOF! 1212 1Why EOF is unset?
ps, I complied this code with Dev-C++



LinkBack URL
About LinkBacks


