CKTIS / C
ferror — C23 stream function
`ferror(stream)` from `<stdio.h>` tests the error indicator belonging to an open `FILE` stream. It returns nonzero if and only if that indicator is set; it performs no I/O, does not return a specific error code, and does not alter the stream state.
Version: C23
Syntax
int ferror(FILE *stream);ferror(stream) from <stdio.h> tests the error indicator belonging to an open FILE stream. It returns nonzero if and only if that indicator is set; it performs no I/O, does not return a specific error code, and does not alter the stream state.
The example reads past the sole character stored in a tmpfile(). Output 1 0 0 shows that fgetc returned EOF because of end of file, ferror remained zero, and both indicators are clear after clearerr.
An input function’s EOF result is ambiguous, so only feof and ferror distinguish exhausted input from a read failure. ferror neither clears the indicator nor supplies an operating-system cause; preserve any relevant errno and use clearerr before a deliberate retry. stream must designate a valid open stream.
Examples
#include <stdio.h>
int main(void) {
FILE *stream = tmpfile();
if (stream == NULL) return 1;
if (fputc('A', stream) == EOF) { fclose(stream); return 1; }
rewind(stream);
(void)fgetc(stream);
int at_end = fgetc(stream) == EOF && feof(stream) != 0;
int failed = ferror(stream) != 0;
clearerr(stream);
printf("%d %d %d\n", at_end, failed, feof(stream) != 0 || ferror(stream) != 0);
return fclose(stream) == 0 ? 0 : 1;
}
Result
1 0 0
The example reads past the sole character stored in a `tmpfile()`. Output `1 0 0` shows that `fgetc` returned `EOF` because of end of file, `ferror` remained zero, and both indicators are clear after `clearerr`.