INFO: fflush() & flushall() Do Not Write Data Directly to Disk

This article was previously published under Q66052
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
The fflush() and flushall() functions in the C run-time library do notwrite file changes directly to disk. These functions flush the file buffersprovided by the library; they do not flush the buffers the MS-DOS, OS/2, orWindows NT operating systems provide at the system level. For example, ifan application calls fflush() but MS-DOS does not write its buffers to diskbefore a system crash (or equivalent event), information may still be lost. To address this situation in MS-DOS, use the _commit() function after youcall fflush() or fflushall(). Calling _commit() causes the operating systemto flush to disk the buffer associated with a file handle.

The OS/2 operating system provides two separate functions, depending on theinstalled version of OS/2. In 16-bit OS/2 (versions 1.x), use theDosBufReset() function. In 32-bit OS/2 (version 2.0), use theDosResetBuffer() function.

The Windows NT operating system provides the WIN32 API FlushFileBuffers().The _commit() function included with the Visual C++ 32-bit Edition CRTcalls FlushFileBuffers to write buffered data to disk.

Microsoft C/C++ version 7.0 introduces the "c" mode option for the fopen()function. When an application opens a file and specifies the "c" mode, therun-time library writes the contents of the file buffer to disk when theapplication calls the fflush() or _flushall() function. The "c" mode optionis a Microsoft extension and is not part of the ANSI standard for fopen().

To change the default behavior of the fflush() and _flushall() functions,link your application with the COMMODE.OBJ file provided by the productslisted at the beginning of this article. COMMODE.OBJ changes the globalcommit flag such that calling fflush() or _flushall() commits the filebuffer to disk. This flag tells fflush() to call _commit() to flush the low-level file when the stream is flushed and _commit() in turn calls_dos_commit() for the MS-DOS level file. By default flushing a stream doesnot flush the low-level and MS-DOS level files.

Article ID: 66052 - Last Review: 12/12/2003 14:52:41 - Revision: 3.0

The C Run-Time (CRT)

  • kbcrt kbinfo KB66052