FileWrite flush

Started by kdmoyers, April 01, 2024, 05:29:52 AM

Previous topic - Next topic

kdmoyers

Quick question: does FileWrite flush to disk right away, or is there some buffering? If there is buffering, is there a way to make it flush?

Thanks!
-Kirby
The mind is everything; What you think, you become.

td

There is no simple answer to this question. Windows has at least three different places where file IO may be cashed. They include the OS kernel, the file system (NTFS for example), and the physical drive.

MSFT does provide methods for turning off file caching on a per-file basis but that comes with multiple caveats.

Here is a link to some documentation on the subject:

https://learn.microsoft.com/en-us/windows/win32/fileio/file-caching

The Win32 API function CreateFileW does provide a FILE_FLAG_NO_BUFFERING flag that turns system buffering off. It does not affect disk caching, however.

You could also consider using the DllCall function to make a call to the FlushFileBuffers function. It has the advantage of also flushing file metadata to disk.

One simple solution is to close the file handle after any file writes when file caching is a concern. Of course, you take a performance hit doing this but performance along with file overlap operations are the reason file cashing exists in the first place.


"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

The FileClose trick sounds good, thanks!!
The mind is everything; What you think, you become.