ferror()

Declaration

int ferror(FILE* qpStream);

Description:

This function is used to test for error conditions on a stream. The argument "qpStream" is passed in for testing. If an error has occured on "qpStream" then the return value is nonzero; otherwise the function returns 0.

Example

#include <cstdio>

int main()
{
    char* cpFileName = "XoaX.txt";
    FILE* qpFile = fopen(cpFileName, "r" );

    // Check that the file was opened
    if (!qpFile) {
        printf("Could not open %s for reading \n",  cpFileName);
        return 1;
    }

    // Create an error by trying to write to the read only file
    putc('a', qpFile);

    // Get the error type
    int iResult = ferror(qpFile);
    if (iResult != 0) {
        printf("Reading Error: %i\n", iResult);
        return 1;
    }

    fclose(qpFile);

    return 0;
}

Input File:

ferror() Input File

Output:

ferror() Output