From the Linux man page of fwrite
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).
so you need to compare with what is expected return value.
In many cases you may need to check for errno
equal to EAGAIN
or EINTR
, in which case you normally want to retry the write request, while in other cases you want to handle short writes gracefully.
For fwrite, on a short write (where less than your entire data was written) you can check feof() and/or ferror() to see if the stream is returning and end-of-file, EOF, such as if a PIPE was closed, or if the stream has its error inducator flag set.