The fwrite()
function shall write, from the array pointed to by ptr
, up to nitems
elements whose size is specified by size
, to stream
.
For each object( nitems
times for a fwrite()
call), size
calls shall be made to the fputc()
, taking the values (in order) from an array of unsigned char exactly overlaying the object.
As long as the underlying writes are buffered (which is commonly the case for most filesystems), the difference in performance of both the cases will be quite negligible.
The file-position indicator for the stream (if defined) shall be advanced by the number of bytes successfully written. If an error occurs, the resulting value of the file-position indicator for the stream is unspecified.
How the two variants differ is when partial writes are performed and fwrite()
returns. Then having a smaller size
and larger nitems
provides a higher granularity in the return value.
For example if ONLY 5 bytes were written (in an attempt to write 10 bytes),
// case 1
fwrite(bufptr, 1, 10, fptr);
// returns 5. As 5 out of 10 items written successfully
// case 2
fwrite(bufptr, 10, 1, fptr);
// returns 0. As 0 out of 1 item written