home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / direct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-03  |  1.8 KB  |  65 lines

  1. /* DIRECT.C illustrates directory functions and miscellaneous file
  2.  * and other functions including:
  3.  *      getcwd          mkdir           chdir           rmdir
  4.  *      system          mktemp          remove          unlink
  5.  *      stat
  6.  *
  7.  * See NULLFILE.C for an example of fstat, which is similar to stat.
  8.  */
  9.  
  10. #include <direct.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <io.h>
  15. #include <time.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18.  
  19. main()
  20. {
  21.     char cwd[_MAX_DIR];
  22.     static char tmpdir[] = "DRXXXXXX";
  23.     struct stat filestat;
  24.  
  25.     /* Get the current working directory. */
  26.     getcwd( cwd, _MAX_DIR );
  27.  
  28.     /* Try to make temporary name for directory. */
  29.     if( mktemp( tmpdir ) == NULL )
  30.     {
  31.         perror( "Can't make temporary directory" );
  32.         exit( 1 );
  33.     }
  34.  
  35.     /* Try to create a new directory, and if successful, change to it. */
  36.     if( !mkdir( tmpdir ) )
  37.     {
  38.         chdir( tmpdir );
  39.  
  40.         /* Create and display a file to prove it. */
  41.         system( "echo This is a test. > TEST.TXT" );
  42.         system( "type test.txt" );
  43.  
  44.         /* Display some file statistics. */
  45.         if( !stat( "TEST.TXT", &filestat ) )
  46.         {
  47.             printf( "File: TEST.TXT\n" );
  48.             printf( "Drive %c:\n", filestat.st_dev + 'A' );
  49.             printf( "Directory: %s\\%s\n", cwd + 2, tmpdir );
  50.             printf( "Size: %ld\n", filestat.st_size );
  51.             printf( "Created: %s", ctime( &filestat.st_atime ) );
  52.         }
  53.         getch();
  54.  
  55.         /* Delete file, go back to original directory, and remove
  56.          * directory. Note that under DOS, remove is equivalent to unlink,
  57.          * so this line could be used instead.
  58.         unlink( "TEST.TXT" );
  59.          */
  60.         remove( "TEST.TXT" );
  61.         chdir( cwd );
  62.         rmdir( tmpdir );
  63.     }
  64. }
  65.