I'm trying to understand when the stdio function clearerr()
should be used.
For example, if I fread()
or fwrite()
on a valid FILE*
and get a short count and ferror
is true, what can I do?
From what I've read so far, fread()
and fwrite()
are robust and will block and/or retry (if there are locks and/or interrupts which could happen in lower level functions) so there never seems any point in using clearerr()
because fread
or fwrite
errors will be so catastrophic there is no point in trying to recover.
Additionally, ferror()
only tells me that there is an error, not what the error is.
#define SZ 1024
FILE* fp = fopen( "foo", "r" );
if ( fp ) {
char b[SZ];
int ch_count = fread( b, sizeof(char), SZ, fp );
if ( ch_count != SZ && ferror( fp ) ) {
// how would clearerr() be used. I don't know?
// ....
// should I drop through here to fclose? (when I've got an ferror)
}
fclose( fp );
}