C++ clearerr()

The clearerr() function in C++ resets the error flags and the EOF indicator for the given file stream.

clearerr() prototype

void clearerr(FILE* stream);

It is defined in <cstdio> header file.

clearerr() Parameters

stream: The file stream to reset the error flags and EOF indicator.

clearerr() Return value

None.

Example: How clearerr() function works

#include <iostream>
#include <cstdio>

using namespace std;

int main ()
{
    int ch;
    FILE* fp;
    fp = fopen("file.txt","w");
    
    if(fp)
    {
        ch = getc(fp);
        if(ferror(fp))
        {
            cout << "Error set" << endl;
            clearerr (fp);
        }
    }
    if(!ferror(fp))
        cout << "Error reset";
    fclose (fp);
    return 0;
}

When you run the program, the output will be:

Error set
Error reset