Displaying time and date of files

This is a discussion on Displaying time and date of files within the C Programming forums, part of the General Programming Boards category; Hi, Im having trouble writing a program that asks the user to enter a command say a filename, and the ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    11

    Displaying time and date of files

    Hi,
    Im having trouble writing a program that asks the user to enter a command say a filename, and the program then prints out the access date of that file, and the last modification date
    eg of output
    filename
    last modified on feb 2002 12:20pm
    last accessed on May 5th 08:07am

    Im trying to use clock_t but I cant find any good examples
    Im also looking for a way on how to use the stat command
    any assistance would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    11
    sorry to reply to this again
    but I think I understand my question regarding the use of clock_t

    does anyone know a good example where stat is ued?
    man stat isnt very helpful

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    struct stat statbuff;
    stat( "file.txt", &statbuff );

    Then look in statbuff for the fields which interest you

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <sys/stat.h>
    
    int main(int argc, char *argv[])
    {
    	struct stat buf;
    
    	if (stat(argv[0], &buf) != -1)
    	{
    		printf("File size = %d\n", buf.st_size);
    		printf("File mode = %03o\n", buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
    		printf("File UID = %d\n", buf.st_uid);
    		printf("File GID = %d\n", buf.st_gid);
    		printf("File last accessed = %s", ctime(&buf.st_atime));
    		printf("File last modified = %s", ctime(&buf.st_mtime));
    		printf("File last changed = %s", ctime(&buf.st_ctime));
    	}
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. file's date and time stamp
    By Lord CyKill in forum C++ Programming
    Replies: 8
    Last Post: 04-10-2005, 04:04 AM
  4. displaying date and time
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21