Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Next: , Previous: EOF and Errors, Up: I/O on Streams


12.16 Recovering from errors

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

— Function: void clearerr (FILE *stream)

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)

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.


 
 
  Published under the terms of the GNU General Public License Design by Interspire