fsync()
会在内部执行fflush()
,所以在流上使用fsync()
是可以的。但是当在网络I/O下执行时,我得到了一个意外的结果。我的代码片段:
FILE* fp = fopen(file, "wb");
/* multiple fputs() calls like: */
fputs(buf, fp);
...
...
fputs(buf.c_str(), fp);
/* get fd of the FILE pointer */
fd = fileno(fp);
#ifndef WIN32
ret = fsync(fd);
#else
ret = _commit(fd);
fclose(fp);
但是似乎_commit()
没有刷新数据(我在Windows上尝试过,数据被写入了一个Linux导出的文件系统)。
当我将代码更改为:
FILE* fp = fopen(file, "wb");
/* multiple fputs() calls like: */
fputs(buf, fp);
...
...
fputs(buf.c_str(), fp);
/* fflush the data */
fflush(fp);
fclose(fp);
这会刷新数据。
我想知道 _commit()
是否和 fflush()
做的是同样一件事。有什么输入吗?
#endif
在哪里? - binki