C examples for stdio.h:clearerr
function
<cstdio> <stdio.h>
void clearerr ( FILE * stream );
Resets both the error and the eof indicators of the stream.
When a i/o function fails, C sets internal indicators.
The value these indicators is cleared by calling clearerr function, or by a call to any of:
The I/O function could fail because of an error or because the end of the file has been reached.
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the stream. |
None
The following program opens an existing file for reading and causes an I/O error trying to write on it.
That error is cleared using clearerr function, so a second error checking returns false.
#include <stdio.h> int main (){//from www. ja v a 2 s .c om FILE * pFile; pFile = fopen("main.cpp","r"); if (pFile==NULL) { perror ("Error opening file"); return -1; } fputc ('a',pFile); if (ferror (pFile)) { printf ("Error Writing to main.cpp\n"); clearerr (pFile); } fgetc (pFile); if (!ferror (pFile)) printf ("No errors reading main.cpp\n"); fclose (pFile); return 0; }