Reputation: 11409
From somewhere I got the code to write something to a file using fwrite on the current file position like this:
fwrite(SomeValue, sizeof(unsigned char), 1, outfile);
I would like to replace the "1" with a constant. I was expecting it to be one like "CUR_POS" or something like that, but did not find anything.
Can somebody help?
Thank you!
Upvotes: 1
Views: 414
Reputation: 24269
The 1
in
fwrite(SomeValue, sizeof(unsigned char), 1, outfile);
is actually a multiplier for the value of sizeof(unsigned char)
and not the position in the file.
It's so that you can easily do something like:
struct Foo bar[64];
int numItems = fwrite(bar, sizeof(Foo) /*objectSize*/, 64 /*numObjects*/, outfile);
"numItems" is then the number of complete 'Foo's it was able to write, which should nominally be 64 in this case.
See http://linux.die.net/man/3/fwrite
For altering the next write position in a FILE* file, see fseek.
Upvotes: 1
Reputation: 36458
It's not a position constant. It's a count. If you were using variables for the parameters, they'd be something like:
fwrite(theData, elementSize, numberOfElements, outfile);
the code you listed is saying to write 1 byte, taken from wherever SomeValue
points, to the current position in outfile
. fwrite
has no way to write anywhere else in the output stream.
If you do want to write elsewhere in a file, you'd look into seek
or lseek
.
Upvotes: 1
Reputation: 57610
The third argument to fwrite
is not the position, as fwrite
always writes to the current position no matter what. The third argument is actually the number of items to write, with the second argument being the size of each item. Thus, if you're just writing a single block of data to a file, the second argument should be just the block's size and the third should be 1
(or vice versa).
Upvotes: 1