C clearerr() Function | Clear Error and EOF Indicators for a File

Introduction

The clearerr() function in C is a standard library function that clears the error and end-of-file indicators for the specified stream. It is part of the C standard library (stdio.h) and is used to reset the error and EOF states of a stream.

clearerr() Function Syntax

The syntax for the clearerr() function is as follows:

void clearerr(FILE *stream);

Parameters:

  • stream: A pointer to a FILE object that specifies the stream for which the error and end-of-file indicators should be cleared.

Returns:

  • The clearerr() function does not return a value.

Examples

Clearing Error Indicators for a File

To demonstrate how to use clearerr() to clear the error indicators for a file, we will write a simple program.

Example

#include <stdio.h>

int main() {
    FILE *file;
    int ch;

    // Open the file for reading
    file = fopen("example.txt", "r");
    if (file == NULL) {
        printf("Error: Could not open file for reading.\n");
        return 1;
    }

    // Attempt to read past the end of the file to set the EOF indicator
    while ((ch = fgetc(file)) != EOF) {
        // Do nothing, just read until EOF
    }

    // Check if EOF indicator is set
    if (feof(file)) {
        printf("End of file reached.\n");
    }

    // Clear the EOF indicator
    clearerr(file);

    // Check if EOF indicator is cleared
    if (!feof(file)) {
        printf("EOF indicator cleared.\n");
    }

    // Close the file
    fclose(file);

    return 0;
}

Output:

End of file reached.
EOF indicator cleared.

Clearing Error Indicators for Standard Input

This example shows how to use clearerr() to clear the error indicators for the standard input stream.

Example

#include <stdio.h>

int main() {
    int ch;

    // Simulate an input error by forcing an EOF
    printf("Enter some text (press Ctrl+D to simulate EOF): ");
    while ((ch = getchar()) != EOF) {
        putchar(ch);
    }

    // Check if EOF indicator is set
    if (feof(stdin)) {
        printf("\nEnd of input reached.\n");
    }

    // Clear the EOF indicator
    clearerr(stdin);

    // Check if EOF indicator is cleared
    if (!feof(stdin)) {
        printf("EOF indicator cleared. You can enter more text now.\n");
    }

    // Read and print more input to demonstrate that the indicator is cleared
    while ((ch = getchar()) != '\n' && ch != EOF) {
        putchar(ch);
    }

    return 0;
}

Output (after pressing Ctrl+D and then entering more text):

Enter some text (press Ctrl+D to simulate EOF): Hello
Hello
End of input reached.
EOF indicator cleared. You can enter more text now.
More text

Real-World Use Case

Robust File Handling

In real-world applications, the clearerr() function can be used to implement robust file handling routines where the program needs to recover from errors or handle end-of-file conditions gracefully.

Example

#include <stdio.h>

void process_file(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error: Could not open file %s for reading.\n", filename);
        return;
    }

    int ch;
    while ((ch = fgetc(file)) != EOF) {
        // Simulate processing each character
        if (ch == 'x') {  // Simulate an error condition
            printf("Encountered 'x', resetting stream...\n");
            clearerr(file);
            // Normally you might handle the error or retry reading
            break;
        }
        putchar(ch);
    }

    if (feof(file)) {
        printf("\nEnd of file reached.\n");
    }
    if (ferror(file)) {
        printf("\nAn error occurred while reading the file.\n");
    }

    fclose(file);
}

int main() {
    process_file("example.txt");
    return 0;
}

Output (assuming example.txt contains the text "Hello, World!"):

Hello, World!

Conclusion

The clearerr() function is used to clear the error and end-of-file indicators for a specified stream. This can be useful in situations where you want to reset the state of a stream after handling an error or reaching the end of the file.

Comments