Different Ways to Write File Information to Disk

ID Number: Q60090

3.30 3.30a 4.00 4.01

MS-DOS

Summary:

When writing information to files opened with MS-DOS function 3CH,

3DH, 5AH, or 3BH, the information is buffered. This data can be lost

if the computer loses power or is rebooted. To prevent this loss of

data from occurring, the buffers need to be flushed to the disk.

Unfortunately, MS-DOS function 0DH, Disk Reset, does not do the job

completely. It writes the buffers to the disk, but doesn't update the

FAT (file allocation table) or the directory header information.

Under MS-DOS Versions 3.30 and later, the function (68H) is available

that performs this type of functionality. Function 68H, Commit File,

will write out the buffers, update the FAT, and update the directory

information.

In earlier versions of MS-DOS that do not include this function, you

could create a duplicate file handle and close that handle. This

yields the same result as using MS-DOS Version 3.30's function 68H.

More Information:

The following is a sample function written in C that will take a file

handle returned from a MS-DOS file handle function and cause the file

information to be updated to the disk:

/* ----------------------------------------------------------------- */

#include <fcntl.h>

#include <sys\types.h>

#include <sys\stat.h>

/*

* int update (hFile)

*

* This function will update the buffers associated with hFile and

* cause the buffers to be written to the disk. If the power fails,

* or the machine is rebooted after this function is executed on an

* integer file handle, all of the information, including the

* directory header, will be up to date.

*

* This function relies on the open() and dup() functions.

*

* Returns:

* 0 - Function was executed correctly

* -1 - Error during execution of function

*/

int update(int hFile)

{

int hFileDup;

/* Duplicate the file handle */

if ((hFileDup = dup(hFile)) == -1)

return (-1);

/* Closing of the duplicate file handle forces the file to be

updated */

return (close (hFileDup));

} /* update */

/* ----------------------------------------------------------------- */