ferror() Function

The ferror() function in C checks whether the error indicator for a stream is set. It is useful for detecting if a previous operation on the stream failed, allowing the programmer to handle errors appropriately.


Syntax of ferror()

</>
Copy
int ferror(FILE *stream);

Parameters

ParameterDescription
streamA pointer to a FILE object that identifies the stream to be checked.

The error indicator associated with the stream is typically set by a previous operation that failed, and it can be cleared by calling clearerr(), rewind(), or freopen().

Return Value

The function returns a nonzero value if the error indicator for the stream is set, and zero if no error has been detected.


Examples for ferror()

Example 1: Checking for Errors After a Successful Operation

This example demonstrates how to use ferror() to check the error indicator after performing a standard file operation.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("example.txt", "w");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Write data to the file
    fputs("Hello, World!\n", fp);

    // Check for errors in the file stream
    if (ferror(fp)) {
        printf("An error occurred while writing to the file.\n");
    } else {
        printf("File written successfully without any errors.\n");
    }

    fclose(fp);
    return 0;
}

Explanation:

  1. The program opens a file named example.txt in write mode.
  2. Data is written to the file using fputs().
  3. ferror() is then used to check if any error occurred during the file operation.
  4. A message is printed indicating whether the file operation was successful.

Program Output:

File written successfully without any errors.

Example 2: Triggering an Error by Writing to a Read-Only File

This example attempts to write to a file opened in read-only mode, which triggers an error that is detected using ferror().

Program

</>
Copy
#include <stdio.h>

int main() {
    // Open file in read mode
    FILE *fp = fopen("example.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Attempt to write to a read-only file to trigger an error
    if (fputs("This will fail", fp) == EOF) {
        // fputs returns EOF on error
    }

    // Check for error indicator using ferror
    if (ferror(fp)) {
        printf("Error detected: Cannot write to a file opened in read mode.\n");
    } else {
        printf("No error detected.\n");
    }

    fclose(fp);
    return 0;
}

Explanation:

  1. The file example.txt is opened in read-only mode.
  2. The program attempts to write to the file using fputs(), which fails and sets the error indicator.
  3. ferror() is then used to detect that an error has occurred.
  4. A message is printed to indicate that writing to a read-only file is not allowed.

Program Output:

Error detected: Cannot write to a file opened in read mode.

Example 3: Clearing an Error Indicator with clearerr()

This example shows how to clear an error indicator using clearerr() after an error has been detected with ferror().

Program

</>
Copy
#include <stdio.h>

int main() {
    // Open file in read-only mode
    FILE *fp = fopen("example.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    // Trigger an error by attempting to write to the read-only file
    fputs("Trigger error", fp);

    // Check for error indicator
    if (ferror(fp)) {
        printf("Error detected before clearerr().\n");
    }

    // Clear the error indicator
    clearerr(fp);

    // Verify that the error indicator has been cleared
    if (!ferror(fp)) {
        printf("Error indicator cleared using clearerr().\n");
    }

    fclose(fp);
    return 0;
}

Explanation:

  1. The file example.txt is opened in read-only mode.
  2. An attempt is made to write to the file, triggering an error.
  3. ferror() is used to detect the error indicator.
  4. clearerr() is then called to clear the error indicator.
  5. A subsequent check confirms that the error indicator has been cleared.

Program Output:

Error detected before clearerr().
Error indicator cleared using clearerr().