FlushFileBuffers

File Management Functions

FlushFileBuffers

The FlushFileBuffers function flushes the buffers of a specified file and returns the number of bytes written.

Syntax

BOOL FlushFileBuffers(
                  HANDLE hFile
                );

Parameters

Parameter Type Description
hFile HANDLE A handle to the open file. The handle must have been created with the GENERIC_WRITE access right.

Return Value

Type Description
BOOL If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

This function writes all buffered data for the specified file to disk. It does not affect the file pointer.

When a process writes data to a file, the system often buffers the data in memory to improve performance. FlushFileBuffers ensures that this buffered data is written to the physical storage device. This is crucial for ensuring data integrity, especially in scenarios where a system might crash or lose power unexpectedly.

If the file handle was opened with FILE_FLAG_WRITE_THROUGH, FlushFileBuffers returns successfully without doing anything. This is because FILE_FLAG_WRITE_THROUGH forces all data to be written through any intermediate cache layers to the target device when writing to the file.

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header fileapi.h (include windows.h)
Library Kernel32.lib
DLL Kernel32.dll

See Also