简体   繁体   English

C编程fwrite跳转到文件末尾

[英]C programming fwrite jumps to end of file

I'm writing a C module and I'm running into an interesting problem I never seen before. 我正在写一个C模块,我遇到了一个我以前从未见过的有趣问题。

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

When I run the module, I get a printout like the following: 当我运行模块时,我得到如下打印输出:

ftell A1 0
count 8
ftell A2 6018

I have set the file pointer to the very start of the file. 我已将文件指针设置为文件的开头。 When I write some data, it should write the data out at the position I seek'd to and then increment the file position with the number of bytes written (in this case, 8). 当我写一些数据时,它应该在我寻找的位置写出数据,然后用写入的字节数增加文件位置(在本例中为8)。 However, when I do an ftell, it seems that the position suddenly jumped to 6018 (which happens to be the original size of the file plus 8). 然而,当我做一个ftell时,似乎该位置突然跳到6018(恰好是文件的原始大小加上8)。

Why does this occur and how do I prevent this behavior? 为什么会发生这种情况,如何防止这种行为?

It sounds like the file has been opened in append mode. 听起来这个文件已经在附加模式下打开了。 Check that there's no "a" in the second argument to fopen() . 检查fopen()的第二个参数中是否没有"a"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM