如何讓WriteFile立即生效

WriteFile函數通常是將數據寫入到內部緩衝區,然後OS會定期將緩衝區中的數據寫入到磁盤。如果想在調用WriteFile之後,數據就立即寫入磁盤,有如下三種方法:

1. 調用FlushFileBuffers(hFile);

Flushes the buffers of a specified file and causes all buffered data to be written to a 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. 關閉掉句柄

CloseHandle(hFile);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章