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.
NULLFILE.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* NULLFILE.C illustrates these functions:
 *      _chsize          _umask           _setmode
 *      _creat           _fstat
 */
 
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <stdlib.h>
#include <time.h>
 
void main()
{
    int fhandle;
    long fsize;
    struct _stat fstatus;
    char fname[80];
 
    /* Create a file of a specified length. */
    printf( "What dummy file do you want to create: " );
    gets( fname );
    if( !_access( fname, 0 ) )
    {
        printf( "File exists" );
        exit( 1 );
    }
 
    /* Mask out write permission. This means that a later call to open
     * will not be able to set write permission. This is not particularly
     * useful in DOS, but _umask is provided primarily for compatibility
     * with systems (such as UNIX) that allow multiple permission levels.
     */
    _umask( _S_IWRITE );
 
    /* Despite write request, file is read-only because of mask. */
    if( (fhandle = _creat( fname, _S_IREAD | _S_IWRITE )) == -1 )
    {
        printf( "File can't be created" );
        exit( 1 );
    }
 
    /* Since _creat uses the default mode (usually text), you must
     * use _setmode to make sure the mode is binary.
     */
    _setmode( fhandle, _O_BINARY );
 
    printf( "How long do you want the file to be? " );
    scanf( "%ld", &fsize );
    _chsize( fhandle, fsize );
 
    /* Display statistics. */
    _fstat( fhandle, &fstatus );
    printf( "File: %s\n", fname );
    printf( "Size: %ld\n", fstatus.st_size );
    printf( "Drive %c:\n", fstatus.st_dev + 'A' );
    printf( "Permission: %s\n",
            (fstatus.st_mode & _S_IWRITE) ? "Read/Write" : "Read Only" );
    printf( "Created: %s", ctime( &fstatus.st_atime ) );
 
    _close( fhandle );
    exit( 0 );
}
                                    -♦-