clearerr() function : How to reset error indicators in C Programming ?
Declaration :
void clearerr(FILE *stream);
- Resets error indication
- clearerr resets the named stream’s error and end-of-file indicators to 0.
- Once the error indicator is set, stream operations continue to return error status until a call is made to clearerr or rewind.
- The end-of-file indicator is reset with each input operation.
- Return Value : None
Live Example :
#include<stdio.h> int main(void) { FILE *fp; char ch; fp = fopen("temp.txt", "w"); ch = fgetc(fp); printf("%c",ch); if (ferror(fp)) { printf("Error reading from temp.txt"); clearerr(fp); } fclose(fp); return 0; }
Explanation of Program :
fp = fopen("temp.txt", "w"); ch = fgetc(fp); printf("%c",ch);
Intentionally we are reading from file which is opened in writing mode. If error is detected by following statement then it will reset the error code and will flush the error.
if (ferror(fp)) { }