用fread和fwrite函数读写结构体数据,把数据写入文件时,只有汉字部分输出正确,数字部分全市乱码,读取数据时,数字部分显示的好像是内存地址。请高手指点,急求,谢谢。
#include<stdio.h>
#include<stdlib.h>
struct time
{
int year;
int mon;
int day;
int hour;
int min;
float sec;
};
void main()
{
struct time tim[8];
int i;
for(i=0; i<8; i++)
{
tim[i].year = 2000;
tim[i].mon = 1;
tim[i].day = 1;
tim[i].hour = 0;
tim[i].min = 0;
tim[i].sec = 0.0;
}
FILE *fp;
fp = fopen("E:\\C程序\\1.txt","wb");
if(fp==NULL)
{
printf("file open error!\n");
exit(0);
}
else
{
for(i=0; i<8; i++)
{
fwrite(&tim[i],sizeof(struct time),1,fp);
printf("%d %d %d %d %d %4.2f\n",
tim[i].year,tim[i].mon,tim[i].day,tim[i].hour,tim[i].min,tim[i].sec);
}
}
fclose(fp);
}
慕丝7291255
哈士奇WWW