How to get file created time using stat() function in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>     

int main()
{
    char file[100] = "d:\\data.txt";
	struct stat stats;
	struct tm dt;

	printf("File: %s\n", file);
	
    if (stat(file, &stats) == 0) {
        dt = *(gmtime(&stats.st_ctime));
		printf("Created on: %d-%d-%d %d:%d:%d", dt.tm_mday, dt.tm_mon, dt.tm_year + 1900, 
                                                dt.tm_hour, dt.tm_min, dt.tm_sec);
    }
    else {
        printf("Unable to get file created time\n");
    }
    
    return 0;
}
        
          
          
          
/*
run:
          
File: d:\data.txt
Created on: 12-6-2020 7:35:3
       
*/

 



answered Jul 12, 2020 by avibootz

Related questions

1 answer 178 views
1 answer 194 views
1 answer 678 views
1 answer 255 views
2 answers 203 views
203 views asked Mar 24, 2021 by avibootz
1 answer 220 views
...