我想在 C 中打开一个文件,其中读取和写入都是同步的。是正确的方法吗
fopen("file.txt", O_DSYNCH | O_RSYNCH)
或
fopen("file.txt", O_SYNCH)
这是在 Linux 上使用
最佳答案
来自man 3 open
:
If both
O_DSYNC
andO_RSYNC
are set inoflag
, all I/O operations on the file descriptor shall complete as defined by synchronized I/O data integrity completion.
因此,正确的调用是
open("file.txt", O_DSYNC | O_RSYNC);
请注意,fopen
不采用 O_
标志(它使用模式字符串,如 "r+"
),因此您不能使用任何直接使用 O_*SYNC
选项。
关于C文件同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079694/