Article ID: 148505
Article Last Modified on 7/1/2004
/* Compile options needed: none
This sample code is designed to show some of the more
common ways to flush both the C runtime buffers and the
operating system cache
*/
#include <stdio.h>
#include <ofstream.h>
void DoSomeCFileIO();
void DoSomeCXXFileIO();
void main()
{
DoSomeCFileIO()
DoSomeCXXFileIO();
}
void DoSomeCFileIO()
{
FILE* CFileBuf;
// Open CFileBuf for output & perform some writes
fflush(CFileBuf);
_commit(_fileno(CFileBuf));
// The call to fflush will cause the C Runtime to flush
// the buffers associated with CFileBuf to the
// Operating system.
// The call to _commit will tell Smartdrv.exe to flush
// its cache to the disk.
// The _commit function requires a file handle, hence the
// call to _fileno
// _commit will only function on 16-bit operating systems.
// On 32-bit operating systems, you need to link to Commode.obj
}
void DoSomeCXXFileIO()
{
ofstream CXXFileBuf;
// Open CXXFileBuf & perform some writes
CXXFileBuf.flush();
_commit(CXXFileBuf.rdbuf()->fd());
// The call to flush causes the C Runtime to flush
// the buffer associated with CXXFileBuf to the operating system.
//
// The call to _commit tells Smartdrv.exe to flush
// its cache to the disk.
//
// The _commit function requires a file handle, hence the
// call to ofstream.rdbuf()->fd()
// _commit will only function on 16-bit operating systesm.
// On 32-bit operating systems, you will need to link to Commode.obj.
}
Keywords: kbhowto kbcrt kbcode KB148505