If a call to fread() returns 0 and ferror() indicates an error (vs. EOF), is it OK to retry the read or is it better to close and reopen the file?
I can’t start over entirely — the input file has been partially processed in a way that can’t be undone (say I’m writing out a chunk at a time to a socket and, due to existing protocol, have no way of telling the remote end, ‘never mind, I need to start over’).
I could fclose() and fopen() the file, fseek() past the data already processed, and continue the fread()-ing from there, but is all that necessary?
There’s no ‘one size fits all’ solution, since different errors can require different handling. Errors from
fread()are unusual; if you’re calling it correctly, an error may indicate a situation that has left theFILE*in a weird error state. In that case you’re best off callingfclose(),fopen(),fseek()to get things back in a good state.If you’re coding for something that’s happening, please mention the actual errors you’re getting from
ferror()…