### 12.16 Recovering from errors

You may explicitly clear the error and EOF flags with the `clearerr` function.

— Function: void clearerr (FILE *stream)

> Preliminary: | MT-Safe | AS-Safe | AC-Unsafe lock | See POSIX Safety Concepts.

> This function clears the end-of-file and error indicators for the stream stream.

> The file positioning functions (see File Positioning) also clear the end-of-file indicator for the stream.

— Function: void clearerr_unlocked (FILE *stream)

> Preliminary: | MT-Safe race:stream | AS-Safe | AC-Safe | See POSIX Safety Concepts.

> The `clearerr_unlocked` function is equivalent to the `clearerr` function except that it does not implicitly lock the stream.

> This function is a GNU extension.

Note that it is not correct to just clear the error flag and retry a failed stream operation. After a failed write, any number of characters since the last buffer flush may have been committed to the file, while some buffered data may have been discarded. Merely retrying can thus cause lost or repeated data.

A failed read may leave the file pointer in an inappropriate position for a second try. In both cases, you should seek to a known position before retrying.

Most errors that can happen are not recoverable — a second try will always fail again in the same way. So usually it is best to give up and report the error to the user, rather than install complicated recovery logic.

One important exception is `EINTR` (see Interrupted Primitives). Many stream I/O implementations will treat it as an ordinary error, which can be quite inconvenient. You can avoid this hassle by installing all signals with the `SA_RESTART` flag.

For similar reasons, setting nonblocking I/O on a stream's file descriptor is not usually advisable.