如何讓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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM