1、
#include <stdio.h> #include <time.h> char file[] = "datatime.dat"; void put_data(void) { FILE *fp; if((fp = fopen(file,"r")) == NULL) // 未检测到datatime.dat文件,显示第一次运行 printf("athe program is run for the first time."); else // 若是检测到datatime.dat文件,读取文件内容并显现 { int year, month, day, h, m, s; fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s); printf("last run time is: %d-%d-%d-%d-%d-%d ", year, month, day, h, m, s); fclose(fp); } } void get_data(void) { FILE *fp; time_t current = time(NULL); struct tm *timer = localtime(¤t); if((fp = fopen(file, "w")) == NULL) printf("afile open failed. "); else { fprintf(fp, "%d %d %d %d %d %d ", timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, timer -> tm_hour, timer -> tm_min, timer -> tm_sec); // 将程序运行的时间写入到文件datatime.dat中 fclose(fp); } } int main(void) { put_data(); get_data(); return 0; }