c++ - 如何刷新 Windows 中的所有文件缓冲区?

标签 c++ c file winapi buffer

在 Windows 中有 FlushFileBuffers() API 可以为 单个 文件刷新缓冲区到硬盘驱动器。 Linux 中有 sync() API 可以刷新所有 文件的文件缓冲区。

但是,是否也有用于刷新所有文件的 WinAPI,即 sync() 模拟?

最佳答案

https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-flushfilebuffers

可以刷新整个硬盘驱动器。

To flush all open files on a volume, call FlushFileBuffers with a handle to the volume. The caller must have administrative privileges. For more information, see Running with Special Privileges.

此外,同一篇文章还说明了如果由于某种原因必须刷新数据时应遵循的正确过程:CreateFile 函数以及 FILE_FLAG_NO_BUFFERINGFILE_FLAG_WRITE_THROUGH 标志。

Due to disk caching interactions within the system, the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media, the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O, call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information, see CreateFile.

还要检查file buffering的限制关于内存和数据对齐。

关于c++ - 如何刷新 Windows 中的所有文件缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54064811/

相关文章:

c++ - nodeMCU 上的持久 https 连接

c++ - Qt 中的 "EXC_BAD_ACCESS"

c - 是否可以使用 C 为函数中的参数创建默认值?

c#检查给定文件路径是否包含根目录

c++ - 如何设置二维窗口

c++ - "x = new(Foo)"与任意 Foo 的 "x = new Foo"相同吗?

c - 在理解 C 中的内存分配时遇到了一些麻烦

c - 使用字符串缓冲区读写文件

android - 以编程方式获取从图库中选择的图像的大小(以 kb 或 mb 为单位)

c - 如何将文件中的数据分配给数据结构