fstream :: flush()或fstream :: sync()是否保证数据已写入磁盘?

时间:2018-03-27 07:20:25

标签: c++ buffer fstream disk ofstream

我正在使用C ++编写一个迷你数据库。在日志模块中,我使用fstream将数据附加到日志文件,我需要将数据立即写入磁盘。在http://www.cplusplus.com/doc/tutorial/files/中,我找到了

  

刷新缓冲区时,其中包含的所有数据都将写入物理介质。

但在http://www.cplusplus.com/forum/general/7343/

  

我已经尝试过flush()调用,但这只是将程序的缓冲区刷新到文件系统的缓冲区中,并不能保证数据物理写入磁盘。

如果flush()或sync()调用保证数据已写入磁盘,我很困惑,例如

std::fstream file;
file.open("...", std::ios::out | std::ios::app); // open a file
file << "..."; //append log
file.flush(); //or file.sync()
//file.flush() has returned
//system crashed(not only program crashed) or an interruption in the supply of electricity heppened 

然后当我重新启动计算机时,日志是否丢失?如果日志丢失,fstream是否有办法强制数据写入磁盘?

0 个答案:

没有答案