c语言 13-12
1、
#include <stdio.h> #include <time.h> char data_file[] = "datatime.dat"; void get_data(void) { FILE *fp; if((fp = fopen(data_file, "r")) == NULL) printf("The grogram is running for the first time.\n"); else { int tmp[6]; fread(tmp, sizeof(int), 6, fp); printf("The frogram ran for the last time: %d-%d-%d; %d-%d-%d\n", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5]); fclose(fp); /*int year, month, day, hour, min, sec; fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &hour, &min, &sec); printf("The frogram ran for the last time: %d-%d-%d; %d-%d-%d\n", year, month, day, hour, min, sec); fclose(fp);*/ } } void put_data(void) { FILE *fp; time_t current = time(NULL); struct tm *timer = localtime(¤t); if((fp = fopen(data_file, "w")) == NULL) printf("\aFile open failed.\n"); else { int tmp[6] = {timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, timer -> tm_hour, timer -> tm_min, timer -> tm_sec}; fwrite(tmp, sizeof(int), 6, fp); /*fprintf(fp, "%d %d %d %d %d %d\n", timer -> tm_year + 1900, timer -> tm_mon + 1, timer -> tm_mday, timer -> tm_hour, timer -> tm_min, timer -> tm_sec);*/ fclose(fp); } } int main(void) { get_data(); put_data(); return 0; }