Looking at the spec for fwrite(const void * ptr, size_t size, size_t count, FILE * stream)
, I'm unsure what exactly goes into the size
parameter and the count
parameter when writing binary data, i.e. a chunk of bytes.
Do I have to tell fwrite to write bufLen
blocks of size 1, or should I tell it to write 1 block of size bufLen
? Is any of these better? How does it affect the returned value? How does it affect the behaviour in the case of an error?
If I specify to write one block of size bufLen
, does it always write either the complete data or nothing?
In code, it looks like this:
char* buf = ...;
int bufLen = ...;
FILE* file = ...;
/* alternative 1: */ int writtenByteCount = fwrite(buf, 1, bufLen, file);
/* alternative 2: */ int writtenByteCount = fwrite(buf, bufLen, 1, file);
printf("fwrite wrote %i of %i bytes/blocks", writtenByteCount, bufLen);
if (writtenByteCount < bufLen) {
// handle error
}