#include <stdio.h>
#include <time.h>
char data_file[]="D:\\%\\datetime.dat";
void get_data(void)
{
FILE *fp;
time_t t;
if((fp = fopen(data_file,"r")) == NULL)
printf("本程序第一次运行!\n");
else {
fread(&t,sizeof(time_t),1,fp);
struct tm *local;
time(&t);
local = localtime(&t);
printf("%d %d %d %d %d %d \n",local->tm_year,local->tm_mon,
local->tm_mday,local->tm_hour,local->tm_min,local->tm_sec);
fclose(fp);
}
}
void put_data(void)
{
FILE *fp;
time_t t;
if((fp = fopne(data_file)) == NULL)
printf("\a文件打开失败\n");
else {
fwrite(&t,sizeof(time_t),1,fp);
fclose(fp);
}
}
int main(void)
{
get_data();
put_data();
return 0;
}
将日期和时间作为 struct tm型的值直接向二进制文件进行读写
时间: 2024-10-09 20:44:59