Testing for Errors Using stdio(3)

One area that is often overlooked in various texts that describe the stdio(3) set of routines is the proper treatment of errors. You have already seen how to discriminate between an error return and a success return with the fopen(3) call. Immediately after a fopen(3) failure, the value of errno contains the reason for the open failure. However, the situation may not be so clear in other circumstances, which will be examined next.

Pitfalls of the ferror(3) Function

By way of review, examine the function synopsis for the ferror(3) function:

#include <stdio.h>

int ferror(FILE *stream);                  /* Test stream for an error */
void clearerr(FILE *stream);               /* Clear an error indication */

The ferror(3) function returns a ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.