Home

ferror

ferror is a function in the C standard library that reports whether the error indicator of a FILE stream is set. It is declared in stdio.h and takes a pointer to a FILE object as its argument. The function is used to determine if a recent I/O operation on the stream failed.

The function returns a nonzero value if the stream’s error indicator is set, and zero if it

Importantly, the error indicator is not automatically cleared by successful I/O. To reset the stream’s error

Usage typically involves checking ferror after a loop that reads from or writes to a file, especially

Relation to errno: some implementations may set errno on an I/O error, but the C standard does

is
not.
The
error
indicator
is
set
by
a
read
or
write
operation
that
fails,
or
by
library
routines
that
signal
an
error
on
the
stream.
This
indicator
is
separate
from
the
end-of-file
indicator.
and
end-of-file
indicators,
you
can
call
clearerr
on
the
stream.
After
an
I/O
sequence,
ferror
and
feof
can
be
used
together
to
distinguish
between
an
error
and
reaching
end-of-file;
feof
reports
end-of-file,
while
ferror
reports
an
error
if
one
has
occurred.
if
the
loop
exits
without
an
explicit
error
return.
If
ferror
indicates
an
error,
the
program
can
handle
or
report
it;
if
feof
is
set
and
ferror
is
not,
the
loop
ended
due
to
end-of-file.
not
require
errno
to
reflect
the
stream’s
error
state.
Therefore,
diagnosing
an
I/O
problem
should
rely
on
the
stream’s
error
indicators
and
any
system-specific
error
information
rather
than
errno
alone.