1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#define _CRT_SECURE_NO_DEPRECATE #include <time.h> #include <stdio.h>
char data_file[] = "datetime.dat";
void get_data(void) { FILE* fp; if ((fp = fopen(data_file, "r")) == NULL) printf("本程序第一次运行。\n"); else { int year, month, day, h, m, s; char mod[10] = {"无"}; fscanf(fp, "%d%d%d%d%d%d%s", &year, &month, &day, &h, &m, &s, mod); printf("上一次运行是在%d年%d月%d日%d时%d分%d秒,心情%s!!\n", year, month, day, h, m, s, mod); 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("\a文件打开失败。\n"); 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); fclose(fp); } }
void mood(void) { FILE* fp; char mod[10] = { 0 };
printf("请输入你今天的心情:"); scanf("%s", mod);
if ((fp = fopen(data_file, "a")) == NULL) printf("\a文件打开失败。\n"); else { fprintf(fp, " %s\n",mod); fclose(fp); } }
int main(void) { get_data(); put_data(); mood(); return 0; }
|