c语言 获取程序上一次运行时间的程序
1、
#include <stdio.h> #include <time.h> void put_data(void) { FILE *fp; if((fp = fopen("time.txt", "r")) == NULL) printf("\aThe program is running for the first time.\n"); else { int year, month, day, week, hour, min, sec; fscanf(fp, "%d%d%d%d%d%d%d", &year, &month, &day, &week, &hour, &min, &sec); printf("The last run of the program: %d-%d-%d; week(%d); %d-%d-%d\n", year, month, day, week, hour, min, sec); fclose(fp); } } void get_data(void) { FILE *fp; time_t current = time(NULL); struct tm *timer = localtime(¤t); if((fp = fopen("time.txt", "w")) == NULL) printf("\aFile open failed.\n"); else { fprintf(fp, "%d %d %d %d %d %d %d\n", timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, timer -> tm_wday, timer -> tm_hour, timer -> tm_min, timer -> tm_sec); fclose(fp); } } int main(void) { put_data(); get_data(); return 0; }