Reputation: 3810
Hi fwrite()'s signature is as follows:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
http://msdn.microsoft.com/en-us/library/h9t88zwz.aspx
I didnt understand the difference between 2nd parameter "size" & 3rd parameter "count"?
Does it mean size of the data copied from buffer to file is of size "size". And what role does count has to play here. ??
Upvotes: 2
Views: 2155
Reputation: 361692
Suppose you've an array as:
T a[N]; //T is the type of each element
//and N is the number of elements in the array
//fill the array here
Now, you want to write this array using fwrite
, so you pass a
as first argument as:
fwrite(a, ..... );
Now how will fwrite
know how many bytes it needs to write? It cannot know this from its first argument because a
gets converted into const void*
which loses lots of useful information, such as the type each elements in the array1, thereby it could have calculated the size of each element, but this information is lost the moment you pass a
as first argument. So you need to pass the size of each element as:
fwrite(a, sizeof(T), ....);
Now fwrite
knows the address (or pointer) from where it will read the elements, and it also knows the size of each element. But it still doesn't know how many elements. So you pass the number of elements in the array, as:
fwrite(a, sizeof(T), N, ...);
And now it knows everything about the data. Now all it needs to know is the file-pointer. So here you write it as:
FILE *fp = /*.....*/ ;
fwrite(a, sizeof(T), N, fp);
Hope that helps.
1. When a
converts into const void*
, it also loses another useful information which is the number of elements. But in C, you cannot pass an array as argument. When you do so, it will decay into pointer, losing the information about the size of the array. fwrite
is a function from C library. However, if you're using fwrite
in C++, then you can wrap it in a function template as:
template<typename T, size_t N>
size_t fwrite(T (&a)[N], FILE *fp)
{
return fwrite(a, sizeof(T), N, fp); //call the C function here
}
Then you can simply call it as:
int a={1,2,3,4,5,6,7};
double b={10,20,30,40,50,60,70};
fwrite(a,fp);
fwrite(b,fp);
Looks good? But you cannot pass a pointer to this:
float *c = new float[100];
fwrite(c,fp); //compilation error
It is because our fwrite
function template will accept only array, not pointer.
Upvotes: 3
Reputation: 308492
Size is the size of each element you want to write. If it's bytes you should use 1
, otherwise use sizeof(whatever)
.
Count is the number of those elements you want to write.
Upvotes: 1
Reputation: 1066
you're writing COUNT things, each one of which is of size SIZE.
So if you have an array of 50 FooBar structures, you would do:
fwrite( some_pointer, sizeof(FooBar), 50, some_stream );
So you could always set size to 1, and instead write:
fwrite( some_pointer, 1, 50 * sizeof(FooBar), some_stream);
but I believe the first way is, if nothing else, much easier to read and less error prone.
Upvotes: 3