/* List13-4をもとにして、日付・時刻をstruct tm型の値として 直接バイナリファイルに読み書きするように変更したプログラムを作成せよ。 */ #include #include char data_file[] = "datetime.bin"; void get_bdata(void) { FILE *fp; struct tm times; if((fp = fopen(data_file, "rb")) == NULL) puts("\a本プログラムを実行するのは初めてですね。"); else { fread(×, sizeof(struct tm), 1, fp); printf("前回は%4d年%02d月%02d日%02d時%02d分%02d秒でした。\n", times.tm_year + 1900, times.tm_mon + 1, times.tm_mday, times.tm_hour, times.tm_min, times.tm_sec); fclose(fp); } } void put_bdata(void) { FILE *fp; time_t t; struct tm *local; time(&t); local = localtime(&t); if((fp = fopen(data_file, "wb")) == NULL) puts("\aファイルをオープン出来ません。"); else { fwrite(local, sizeof(struct tm), 1, fp); fclose(fp); } } int main(void) { get_bdata(); put_bdata(); return 0; }