Thread: accessing file properties

  1. #1
    Registered User subodh_dg's Avatar
    Join Date
    Mar 2005
    Location
    Nagpur, India
    Posts
    4

    accessing file properties

    By which function I can access file properties such as date created, date modified, attributes, size both in Turbo C and VC++ 6.0

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    I guess you'd have to write this function and I can't think of a 'C standard' solution off hand. I do think the following should work on VC6 and any TC / TC++ that had stat.h. You might have to make some minor changes as I tested this in DevC++ (4.9.9.2)

    Regards,
    Brian
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <sys/stat.h> 
    #include <time.h> 
    #include <string.h>
    #include <io.h>
    
    int main(int argc, char *argv[])
    {
        struct stat *c; // attribute structure
        time_t t;
        char str[41],timestr[40];
        int err;
    
        printf("Enter filename: ");
        fgets(str, 40, stdin);
        // crop newline out of string
        str[strlen(str) - 1] = '\0';
    
        err = stat(str,c); // get file attributes
        
        if (err == -1)
        {
            printf("\nError in stat\n");
            printf("\nPress `Enter' to continue . . . ");
            getchar();
            return(1);
        }
    
        // show creation date
        t = c->st_ctime;
        strcpy(timestr,ctime(&t));
        printf("\nDate created = %s\n",timestr);
    
        // get modification date
        // GNU C manual: This is the time of the last modification to
        // the contents of the file.
        t = c->st_mtime;
        strcpy(timestr,ctime(&t));
        printf("Date modified = %s\n",timestr);
    
        // get the last time file was accessed
        // GNU C manual: This is the last access time for the file
        t = c->st_atime;
        strcpy(timestr,ctime(&t));
        printf("Time file was last accessed = %s\n",timestr);
        
        // display file size
        printf("File size = %li bytes\n", c->st_size);
    
        #define EXIST 0
        #define WRITE_ONLY 2
        #define READ_ONLY 4
        #define READ_WRITE 6
        
        // Check that file exist:
        if(!access(str, EXIST))
        {
            printf( "\nFile %s exists.\n\n", str );
        
            // Check read and write permissions
            if(!access( str, READ_WRITE))
                printf( "File %s is READ_WRITE.\n", str );
            else
            {
                if(!access( str, WRITE_ONLY))
                    printf( "File %s is WRITE_ONLY.\n", str );
                if(!access( str, READ_ONLY))
                    printf( "File %s is READ_ONLY.\n", str );
            }
        }
        
        printf("\nPress `Enter' to continue . . . ");
        getchar();
        return 0;
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In Windows, you can get all this information with a call to FindFirstFile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM