`fwrite` 在 `fread` 之后不能直接工作吗? [英] `fwrite` doesn't work directly after `fread`?
问题描述
我有一个程序,它使用 stdio 来读取和写入二进制文件.它缓存当前流位置,并且不会查找读/写偏移量是否已经在所需位置.
I have a program which uses stdio for reading and writing a binary file. It caches the current stream position and will not seek if the read/write offset is already at the desired position.
然而,一个有趣的问题出现了,当读取一个字节并写入下一个字节时,它实际上并没有被写入!
However, an interesting problem appears, that when a byte is read and the following byte is written, it doesn't actually get written!
这是一个重现问题的程序:
Here is a program to reproduce the problem:
#include <cstdio>
int main() {
FILE *f = fopen("test.bin", "wb");
unsigned char d[1024] = { 0 };
fwrite(d, 1, 1024, f);
fclose(f);
f = fopen("test.bin", "rb+");
for (size_t i = 0; i < 1024; i++) {
unsigned char a[1] = { 255 - (unsigned char)(i) };
fflush(f);
fwrite(a, 1, 1, f);
fflush(f);
fseek(f, i, SEEK_SET);
fread(a, 1, 1, f);
printf("%02X ", a[0]);
}
fclose(f);
return 0;
}
你应该看到它把字节FF
写到00
,但是只写了第一个字节,因为它没有跟随fread代码>立即.
You are supposed to see it write the bytes FF
down to 00
, however only the first byte is written because it does not follow a fread
immediately.
如果它在 fwrite
之前寻找,它的行为是正确的.
If it seeks before fwrite
, it acts correctly.
该问题发生在 Visual Studio 2010/2012 和 TDM-GCC 4.7.1 (Windows) 上,但是它适用于 codepad 我猜是因为它是在 Linux 上执行的.
The problem happens on Visual Studio 2010/2012 and TDM-GCC 4.7.1 (Windows), however it works on codepad which I guess is due to it being executed on Linux.
知道为什么会这样吗?
推荐答案
C99 §7.18.5.3/6(引自 N869 最终草案):
C99 §7.18.5.3/6 (quoted from N869 final draft):
“当文件以更新模式打开时('+'作为第二个或第三个字符上面的模式参数值列表)[…] 输入不应直接跟随输出,而没有对文件定位函数的干预调用,除非输入操作遇到结束文件.”
“When a file is opened with update mode (’+’ as the second or third character in the above list of mode argument values) […] input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file.”
这篇关于`fwrite` 在 `fread` 之后不能直接工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!