Join our newsletter for the latest updates.

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


Example: How clearerr() function works

#include <iostream>
#include <cstdio>

using namespace std;

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

When you run the program, the output will be:

Error set
Error reset