qc.hlp (Table of Contents; Topic list)
DIRECT.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* 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 );
    }
}