clearerr() in C

Last Updated : 3 Feb, 2025

In C, file handling errors can be handled using ferror() and feof() functions. But these error flags persists until they are cleared. clearerr() is a built-in function used to reset errors when you are doing read-and-write operations with the file.

Example:

C
#include <stdio.h>

int main() {
    FILE *fptr = fopen("gfg.text", "rw+");
    
    // Reset errors and EOF
    clearerr(fptr);
    if(!feof(fptr))
        printf("EOF reset successfully");
    return 0;
}


Output

EOF reset successfully

Syntax

clearerr() is a standard library function defined in <stdio.h> file.

clearerr(fptr)

Parameters:

  • fptr: File pointer pointing to the file stream.

Return Value:

  • This function does not return any value.

Example of clearerr()

C
#include <stdio.h>

int main() {
    FILE *fptr = fopen("gfg.txt", "w+");
    fprintf(fptr, "GeeksForGeeks!");
    while (fgetc(fptr) != EOF);
    
    if(feof(fptr)){
        printf("EOF ancounter \n");
    }
    
  	// Reset EOF using clearerr
    clearerr(fptr);
    if(!feof(fptr)){
        printf("Reset the EOF successfully");
    }
    
    fclose(fptr);
    return 0;
}

Output
EOF ancounter 
Reset the EOF successfully
Comment