为什么文件没有更新

时间:2016-08-14 17:19:51

标签: c printf

我正在尝试学习并编写了一个小程序,应该定期将文本写入文件。

$ cat test.c

inner

文件#include <stdio.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> int main() { printf("First\n"); errno = 0; FILE *f = fopen("test.file", "a"); int c = 0; //int fd = 0; if (f == NULL) { printf("Something went wrong %d\n", errno); return 1; } //fd = fileno(f); printf("Starting loop\n"); while(1) { c = fprintf(f, "12345\n"); //syncfs(fd); printf("print %d, %d\n", c, errno); sleep(10); } return 0; } 已存在且不为空。我用

编译test.file
test.c

问题是$ gcc -Wall test.c 没有更新,尽管似乎没有任何错误。样本输出

test.file

所以它说$ ./a.out First Starting loop print 6, 0 print 6, 0 写了6个字节而fprintf()为零是正确的。

我虽然它可能与文件系统缓存有关(我实际上是想了解文件系统,所以这个想法可能完全是疯了),所以我添加了对errno的调用以强制写入数据磁盘。此部分在上面的源代码中已注释掉。它仍然不起作用。

如果我将syncfs()中的模式从fopen()更改为a并重新运行该程序,则w将被清除,因此我正在查看正确的文件。< / p>

为什么它没有附加?

test.file

上有gcc 5.4.0

0 个答案:

没有答案
相关问题