1

关闭 fstream 是否保证与文件系统同步?我正在与一位同事讨论这个问题,需要一个明确的参考。我对标准所说的内容以及在使用 Visual Studio 实现的 Windows 上发生的事情感兴趣。

此外,如果关闭并不意味着同步,是否有标准的 C++ 方法来确保同步完成?

4

2 回答 2

1

I'm quite sure it's already sorted out how closing the filestream closes the buffers. So, now, the buffers:

draft of Std'1998: 27.8.1.3.6: basic_filebuf* close();

6 Effects: If is_open() == false, returns a null pointer. If a put area exists, calls overflow(EOF) to flush characters. (...) Finally it closes the file (‘‘as if’’ by calling std::fclose(file)).308) If any of the calls to overflow or std::fclose fails then close fails.

Now, see the overflow(): 27.8.1.4.8 (...)

I've then tried to trace it further, somewhere down there were some references to sync and sputc, but I was unable to trace the exact wording for how overflow guarantees flushing. It surely does, but sorry, my time's up today :/

于 2013-10-22T09:41:36.307 回答
0

std::basic_filebuf::close调用std::fclose,其中指出:“任何未写入的缓冲数据都被刷新到操作系统”,或者根据C99 标准草案 n1256,“流的任何未写入的缓冲数据都被传递到主机环境以写入文件”。

于 2021-10-04T13:34:42.237 回答