
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C Library - clearerr() function
The C library clearerr(FILE *stream) function clears the end-of-file and error indicators for the given stream. This can be useful when you want to reset the state of a file stream after an error or EOF condition has occurred.
Syntax
Following is the C library syntax of the clearerr() function −
void clearerr(FILE *stream);
Parameters
This function accepts only a single parameter −
- stream: A pointer to a FILE object that specifies the stream whose error indicators and EOF flag are to be cleared. This FILE object should be previously opened by functions such as fopen.
Return Value
The clearerr function does not return a value. It is a void function, meaning it performs its operation without providing direct feedback.
Example 1 : Clearing EOF Flag
This example reads the contents of a file until EOF is reached, then clears the EOF flag using clearerr, and confirms that the EOF flag has been cleared.
Below is the illustration of C library clearerr(FILE *stream) function.
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { perror("Failed to open file"); return 1; } char ch; while ((ch = fgetc(file)) != EOF) { putchar(ch); } if (feof(file)) { printf("\nEOF reached.\n"); } clearerr(file); // Clear the EOF flag if (feof(file)) { printf("EOF still set.\n"); } else { printf("EOF cleared.\n"); } fclose(file); return 0; }
Output
The above code produces following result−
<contents of example.txt> EOF reached. EOF cleared.
Example 2: Resetting Stream State for Re-reading
This example reads the contents of a file until EOF is reached, clears the EOF flag with clearerr, rewinds the file to the beginning, and re-reads the file content to show that the stream state has been successfully reset.
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { perror("Failed to open file"); return 1; } char ch; while ((ch = fgetc(file)) != EOF) { putchar(ch); } if (feof(file)) { printf("\nEOF reached.\n"); } clearerr(file); // Clear the EOF flag rewind(file); // Reset file position to the beginning printf("Re-reading the file:\n"); while ((ch = fgetc(file)) != EOF) { putchar(ch); } fclose(file); return 0; }
Output
After execution of above code, we get the following result
<contents of example.txt> EOF reached. Re-reading the file: <contents of example.txt>