fwrite function is used for writing in binary.
fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
here size in "size_t size" represent byte i.e. fwrite() writes nmemb elements of data, each "size" bytes long.
"size" can be 1 byte or can be more. So for int which is of 2 bytes, if we gave 'size' 1 byte, how that integer is written? May be written integer is wrong? Or we should take care of it while writing integer and set value of size equal to 2?
When you write an int
array using fwrite
, you will need to use:
size_t num = fwrite(array, sizeof(int), arraySize, file);
if ( num != arraySize )
{
// Deal with the error condition.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With