关于WriteFile有一点疑问
WriteRile是不是关闭掉句柄之后,数据才真正写入
如何让写入立即生效
[解决办法]
方案1:
在需要刷新的时候调用
The FlushFileBuffers function clears the buffers for the specified file and causes all buffered data to be written to the file.
BOOL FlushFileBuffers(
HANDLE hFile // open handle to file whose buffers are to be flushed
);
方案2:
在用CreateFile创建文件的时候,第6个参数使用标志
FILE_FLAG_WRITE_THROUGH
Instructs the operating system to write through any intermediate cache and go directly to disk. The operating system can still cache write operations, but cannot lazily flush them.
方案3:
关闭掉句柄
[解决办法]
WriteFile函数通常是将数据写入到内部缓冲区
然后OS会定期将缓冲区中的数据写入磁盘
楼主可以在WriteFile之后调用FlushFileBuffers(hFile)
该函数的作用是将指定文件的缓存信息写入磁盘
这样WriteFile写入到缓冲区中的内容就更新到磁盘文件了