Fwrite puts an error indicator in a stream in C

In what cases does the fwrite function put an error indicator in a stream, so that ferror returns true?

In particular, I wonder if he posted an error when all the bytes were not written successfully.

Indicate the link where you get the information from.

thank

+3
source share
2 answers

If any error occurs, an error indicator for the stream will be set and will not be cleared before the call clearerr. However, due to buffering, it is difficult for stdio functions to report errors. Often, the error will not be visible until a later call, because the buffering data never fails, but subsequent recording after filling the buffer may fail. If you use stdio to write files, the only ways I know how to handle write errors correctly (select one):

  • disable buffering (with setbufor setvbuf- this should be the very first operation performed on FILEafter it is opened or else it has UB)
  • track the last successful one fflushand accept any data after that, perhaps only partially written

, POSIX fwrite ( stdio) errno , . C, errno, .

+5

fwrite man Linux:

RETURN VALUE
   fread() and fwrite() return the number of items successfully read or written
   (i.e., not the number of characters).  If an error occurs, or the end-of-file
   is  reached, the return value is a short item count (or zero).

   fread()  does  not  distinguish between end-of-file and error, and callers
   must use feof(3) and ferror(3) to determine which occurred.

, , errno.

0

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


All Articles