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.
DIRECT.C
                                             Up Contents Index Back
─────Run-Time Library───────────────────────────────────────────────────────
 
/* DIRECT.C illustrates directory functions and miscellaneous file
 * functions including:
 *      _getcwd         _mkdir           _chdir          _rmdir
 *      system          _mktemp          remove          _unlink
 *      _stat
 *
 * See NULLFILE.C for an example of _fstat, which is similar to _stat.
 */
 
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#define ANSI                /* Delete for UNIX version. */
 
void main()
{
    char cwd[_MAX_DIR];
    char tmpdir[] = "DRXXXXXX";
    struct _stat filestat;
 
    /* Get the current working directory. */
    _getcwd( cwd, _MAX_DIR );
 
    /* Try to make temporary name for directory. */
    if( _mktemp( tmpdir ) == NULL )
    {
        perror( "Can't make temporary directory" );
        exit( 1 );
    }
 
    /* Try to create a new directory, and if successful, change to it. */
    if( !_mkdir( tmpdir ) )
    {
        _chdir( tmpdir );
 
        /* Create and display a file to prove it. */
        system( "echo This is a test. > TEST.TXT" );
        system( "type test.txt" );
 
        /* Display some file statistics. */
        if( !_stat( "TEST.TXT", &filestat ) )
        {
            printf( "File: TEST.TXT\n" );
            printf( "Drive %c:\n", filestat.st_dev + 'A' );
            printf( "Directory: %s\\%s\n", cwd + 2, tmpdir );
            printf( "Size: %ld\n", filestat.st_size );
            printf( "Created: %s", ctime( &filestat.st_atime ) );
        }
        _getch();
 
        /* Delete file, go back to original directory, and remove
         * directory.
         */
#if defined( ANSI )
        remove( "TEST.TXT" );       /* ANSI compatible */
#else
        _unlink( "TEST.TXT" );      /* UNIX compatible */
#endif
        _chdir( cwd );
        _rmdir( tmpdir );
    }
}
                                    -♦-