Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fwrite function in C

Tags:

c

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?

like image 513
user3717474 Avatar asked Sep 17 '25 18:09

user3717474


1 Answers

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.
}
like image 150
R Sahu Avatar answered Sep 20 '25 10:09

R Sahu