Programming Reference/Librarys
Question & Answer
Q&A is closed
int ferror(FILE *stream);
the function ferror tests the error indicator for the stream pointed to by stream, returning non-zero if it is set. The error indicator can only be reset by the clearerr function. Returns non-zero if error indicator is set for stream stream.
/* * ferror example code * http://code-reference.com/c/stdio.h/ferror */ #include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ int main( void ) { FILE *handle; handle = fopen("test.txt","r"); if (handle!=NULL) { printf ("its all fine\n"); putc('X',handle); if (ferror(handle)) { printf ("error in writing to file\n");} fclose (handle); } else { perror ("error opening file for reading\n"); } return 0; }
if the test.txt does not exist
output: ./ferror error opening file for reading : No such file or directory
if the test.txt exists
output: ./ferror
its all fine
error in writing to file