C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
DCOMMIT.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* DCOMMIT.C illustrates DOS file I/O functions including:
 *      _dos_commit     _dos_creatnew     _dos_write
 *      _dos_creat      _dos_close
 *
 */
 
#include <dos.h>
#include <errno.h>
#include <conio.h>
 
void main()
{
    char saveit[] = "Straight to disk. ",
         prompt[] = "File exists, overwrite? [y|n] ",
         err[] = "Error occurred. ",
         newline[] = "\n\r";
    int hfile, ch;
    unsigned count;
 
    /* Open file and create, overwriting if necessary. */
    if( _dos_creatnew( "COMMIT.LOG", _A_NORMAL, &hfile ) != 0 )
    {
        if( errno == EEXIST )
        {
            /* Use _dos_write to display prompts. Use _bdos to call
             * function 1 to get and echo keystroke.
             */
            _dos_write( 1, prompt, sizeof( prompt ) - 1, &count );
            ch = _bdos( 1, 0, 0 ) & 0x00ff;
            if( (ch == 'y') || (ch == 'Y') )
                _dos_creat( "COMMIT.LOG", _A_NORMAL, &hfile );
            _dos_write( 1, newline, sizeof( newline ) - 1, &count );
        }
    }
    /* Write to file; output passes through
       operating system's buffers. */
    if( _dos_write( hfile, saveit, sizeof( saveit ), &count ) != 0 )
    {
        _dos_write( 1, err, sizeof( err ) - 1, &count );
        _dos_write( 1, newline, sizeof( newline ) - 1, &count );
    }
    /* Write directly to file with no intermediate buffering */
    if( _dos_commit( hfile ) != 0 )
    {
        _dos_write( 1, err, sizeof( err ) - 1, &count );
        _dos_write( 1, newline, sizeof( newline ) - 1, &count );
    }
     /* Close file. */
    if( _dos_close( hfile ) != 0 )
    {
        _dos_write( 1, err, sizeof( err ) - 1, &count );
        _dos_write( 1, newline, sizeof( newline ) - 1, &count );
    }
}
                                    -♦-