C 编程 fwrite 跳转到文件末尾



我正在编写一个C模块,遇到了一个我以前从未见过的有趣问题。

// Many other operations before this point
fseek(samples_file, 0, SEEK_SET);
printf("ftell A1 %llun", ftell(samples_file));
count = fwrite(channel_buffer+chan_type.size*set_index, 1, chan_type.size, samples_file);
printf("count %llun", count);
printf("ftell A2 %llun", ftell(samples_file));
// Many more operations to come after this point

当我运行模块时,我得到如下所示的打印输出:

ftell A1 0
count 8
ftell A2 6018

我已将文件指针设置为文件的开头。当我写入一些数据时,它应该将数据写出我想要的位置,然后随着写入的字节数(在本例中为 8)递增文件位置。但是,当我做一个ftell时,似乎位置突然跳到了6018(恰好是文件的原始大小加8)。

为什么会发生这种情况,如何防止此行为?

听起来文件已在追加模式下打开。检查第二个参数中没有"a" fopen()

最新更新