What is a good way to recover from fread () failure?

If calling the fread () function returns 0 and ferror () indicates an error (against EOF), is it ok to repeat the reading or is it better to close and open the file again?

I can’t start completely - the input file was partially processed in a way that cannot be undone (for example, I write a fragment at a time to the socket and, by virtue of the existing protocol, are not able to tell the remote end, "it doesn’t matter, I need to start all over again") .

I can fclose () and fopen () the file, fseek () past the already processed data and continue fread () - from there, but is all this necessary?

+3
source share
3 answers

" ", . fread() ; , , FILE* . fclose(), fopen(), fseek(), .

-, , , , ferror()...

+6
+6

perror() strerror() , .

It is not mandatory to implement such an error message. You must set errnoto 0 before calling fread(); if it fails and errnois still 0, then the error information will not be available.

0
source

Source: https://habr.com/ru/post/1707051/


All Articles