_commit

Flushes a file directly to disk.

int_commit(inthandle**);**

Routine Required Header Optional Headers Compatibility
_commit <io.h> <errno.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_commit returns 0 if the file was successfully flushed to disk. A return value of –1 indicates an error, and errno is set to EBADF, indicating an invalid file-handle parameter.

Parameter

handle

Handle referring to open file

Remarks

The _commit function forces the operating system to write the file associated with handle to disk. This call ensures that the specified file is flushed immediately, not at the operating system’s discretion.

Example

/* COMMIT.C illustrates low-level file I/O functions including:
 *     _close    _commit    memset    _open    _write
 * This is example code; to keep the code simple and readable
 * return values are not checked.
 */

#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>

#define MAXBUF 32

int log_receivable( int );

void main( void )
{
    int fhandle;
    fhandle = _open( "TRANSACT.LOG", _O_APPEND | _O_CREAT |
                                     _O_BINARY | _O_RDWR );
    log_receivable( fhandle );
    _close( fhandle );
}

int log_receivable( int fhandle )
{
/* The log_receivable function prompts for a name and a monetary
 * amount and places both values into a buffer (buf). The _write
 * function writes the values to the operating system and the
 * _commit function ensures that they are written to a disk file.
 */

    int i;
    char  buf[MAXBUF];

    memset( buf, '\0', MAXBUF );
    /* Begin Transaction. */
    printf( "Enter name: " );
    gets( buf );
    for( i = 1; buf[i] != '\0'; i++ );
    /* Write the value as a '\0' terminated string. */
    _write( fhandle, buf, i+1 );
    printf( "\n" );

    memset( buf, '\0', MAXBUF );
    printf( "Enter amount: $" );
    gets( buf );
    for( i = 1; buf[i] != '\0'; i++ );
    /* Write the value as a '\0' terminated string. */
    _write( fhandle, buf, i+1 );
    printf( "\n" );

    /* The _commit function ensures that two important pieces of
     * data are safely written to disk. The return value of the
     * _commit function is returned to the calling function.
     */
    return _commit( fhandle );
}

Low-Level I/O Routines

See Also   _creat, _open, _read, _write