Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

clearerr — C23 stream-state function

`clearerr(stream)` from `<stdio.h>` clears both the end-of-file indicator and the error indicator belonging to the specified `FILE` stream; it returns no value. It performs no input or output operation.

Version: C23

Syntax

void clearerr(FILE *stream);

clearerr(stream) from <stdio.h> clears both the end-of-file indicator and the error indicator belonging to the specified FILE stream; it returns no value. It performs no input or output operation.

Use it before deliberately attempting more I/O after handling EOF or an error. The example reads past the one stored character in a tmpfile(), observes feof = 1 and ferror = 0, calls clearerr, and then observes 0 0.

clearerr does not move the file position, discard buffered data, or retry the failed operation; later I/O can set either indicator again. stream must point to a live valid FILE object, and diagnostics must distinguish feof from ferror.

Examples

#include <stdio.h>

int main(void) {
    FILE *stream = tmpfile();
    if (stream == NULL) return 1;
    fputs("A", stream);
    rewind(stream);
    (void)fgetc(stream);
    (void)fgetc(stream);
    printf("%d %d ", feof(stream) != 0, ferror(stream) != 0);
    clearerr(stream);
    printf("%d %d\n", feof(stream) != 0, ferror(stream) != 0);
    fclose(stream);
    return 0;
}

Result

1 0 0 0

Use it before deliberately attempting more I/O after handling EOF or an error. The example reads past the one stored character in a `tmpfile()`, observes `feof = 1` and `ferror = 0`, calls `clearerr`, and then observes `0 0`.

Sources