Very simply put, I have the following code snippet:
FILE* test = fopen('C:\\core.u', 'w'); printf('Filepointer at: %d\n', ftell(test)); fwrite(data, size, 1, test); printf('Written: %d bytes.\n', size); fseek(test, 0, SEEK_END); printf('Filepointer is now at %d.\n', ftell(test)); fclose(test);
and it outputs:
Filepointer at: 0 Written: 73105 bytes. Filepointer is now at 74160.
Why is that? Why does the number of bytes written not match the file pointer?
Since you’re opening the file in text mode, it will convert end-of-line markers, such as LF, into CR/LF.
This is likely if you’re running on Windows (and you probably are, given that your file name starts with
'c:\').If you open the file in
'wb'mode, I suspect you’ll find the numbers are identical:The C99 standard has this to say in
7.19.5.3 The fopen function:You can see they distinguish between
wandwb. I don’t believe an implementation is required to treat the two differently but it’s usually safer to use binary mode for binary data.