Thread: stat

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    stat

    how to find from a C program if a file has been accessed or not.....and also how to find from the program if a directory exists or not

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What operating system? Judging by your title I'll assume a POSIX compliant system:
    Code:
    #include <sys/stat.h>
    #include <unistd.h>
    #include <time.h>
    
    #define FILE_NAME "somefile"
    
    int main ( void )
    {
      struct stat file_stats;
    
      if ( stat ( FILE_NAME, &file_stats ) != -1 ) {
        printf ( "%s last accessed on %s", FILE_NAME, ctime ( file_stats.st_atime ) );
    
        if ( S_ISDIR ( file_stats.st_mode ) )
          printf ( "%s is a directory\n", FILE_NAME );
      }
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2007, 04:17 AM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  3. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 05:47 AM
  4. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM
  5. that damn stat()
    By @licomb in forum Linux Programming
    Replies: 2
    Last Post: 08-22-2001, 04:24 PM