End of FILE* pointer is not equal to size of written data

扶醉桌前 提交于 2019-12-06 22:30:25

问题


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?


回答1:


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:

FILE* test = fopen("C:\\core.u", "wb");

The C99 standard has this to say in 7.19.5.3 The fopen function:

The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode. Otherwise, the behaviour is undefined.

r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file
rb open binary file for reading
wb truncate to zero length or create binary file for writing
ab append; open or create binary file for writing at end-of-file
r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at end-of-file
r+b or rb+ open binary file for update (reading and writing)
w+b or wb+ truncate to zero length or create binary file for update
a+b or ab+ append; open or create binary file for update, writing at end-of-file

You can see they distinguish between w and wb. 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.




回答2:


what does fwrite return? normally the return value should be the number of bytes written. Also, what does the ftell() answer with right before the fseek?

It might help to know what operating system, C compiler version and C library.




回答3:


A filepointer is a cookie. It has no value. The only thing you can use it for is to seek to the same place in a file. I'm not even sure if ISO C guarantees that ftell returns increasing values. If you don't believe this, please look at the different seek() modes. They exist precisely because the position is not a simple byte offset.




回答4:


windows doesn't actually write all data out to the file without a flush and possibly an fsync. Maybe that's why



来源:https://stackoverflow.com/questions/145006/end-of-file-pointer-is-not-equal-to-size-of-written-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!