fwrite(buffer,size, count,fp);
fwrite(na, 1, 36, fp)
na is buffer, fp is fp
I want to know what do size and count mean here.. thx!!~
fwrite(buffer,size, count,fp);
fwrite(na, 1, 36, fp)
na is buffer, fp is fp
I want to know what do size and count mean here.. thx!!~
size is the size of the data item you want to write, count is the number of items of that size you want to write.Code:char array[50]; fwrite(array, sizeof(array), 1, file);
The above writes the whole array to the file.
Size would be the size of the record, most commonly you will be using a structure so you would say sizeof(struct Label_Name). And count would be the number of records (structs) you want to write. If you want to write the next 5 recrods (structs) from your array of structures substitute 5 for count.
Code:int array[100]; size_t elem_size = sizeof( int ); // size of each element size_t elem_count= 100; // the number of elements size_t result; result = fwrite( array, elem_size, elem_count, fp ); if ( result != elem_count ) { // panic }
THX!
But I still have questions.
The size is 1 in my example.
so it's very small,quite strange?
what's the unit of size?
and what's the meaning of sizeof(array)?
is that mean the size of the array?
and your count is 1.Is that mean one value will be wrote?
THX!!~
> The size is 1 in my example.
presumably because you have
char na[36];
> what's the unit of size?
a char is the smallest addressable memory unit, and it's size is always 1
Everything else is an integral multiple of this
> and what's the meaning of sizeof(array)?
The size of the whole array
> and your count is 1.Is that mean one value will be wrote?
fwrite(array, sizeof(array), 1, file);
Remember fwrite(buffer,size, count,fp);
In all these cases, the total number of bytes written to the file is given by size*count
So, these are the same (usually)
int array[100];
fwrite(array, sizeof(array), 1, file);
fwrite(array, sizeof(array[0]), 100, file);
fwrite(array, sizeof(int), 100, file);
fwrite(array, sizeof(array[0]), sizeof(array)/sizeof(array[0]), file);
The only minor difference between them is the return result - fwrite should return the value of count, if all is well, so in one case it would return 1, but in the others, it would return 100.
The major difference between them is what happens if there is an error writing the file. In the first case, the count is 1, so either the whole array is written or it isn't.
In the other cases, the count is 100, so any number of elements of the array (from 0 to 100) could be written - the number written being the return result. Perhaps you could consider saving some of the data to be preferable to saving none of it.