通过结构体保存的文件,输出到显示器是正确的,但是直接查看文件是乱码,为什么?要怎么改?
#include<stdio.h>#include<string.h>
#include<stdlib.h>
struct student
{
char name[10];
long num;
char sex[10];
int age;
char adrr[10];
int pay;
char health[10];
char classa[10];
}a[5];
struct jian
{
char name[10];
int pay;
}b[5];
void main()
{
FILE *fp1,*fp2,*fp3;
int i;
fp1=fopen("x1.txt","wb");
for(i=0;i<=4;i++)
scanf("%s%ld%s%d%s%d%s%s",&a[i].name,&a[i].num,&a[i].sex,&a[i].age,&a[i].adrr,&a[i].pay,&a[i].health,&a[i].classa);
for(i=0;i<5;i++)
fwrite(&a[i],sizeof(struct student),1,fp1);
fclose(fp1);
//以上是把数据写入x1.txt
fp2=fopen("x1.txt","rb");
for(i=0;i<=4;i++)
{
fread(&a[i],sizeof(struct student),1,fp2);
strcpy(b[i].name,a[i].name);
b[i].pay=a[i].pay;
}
fclose(fp2);
//以上是从x1.txt读数据,赋给b[5]
fp3=fopen("x22.txt","wb");
for(i=0;i<=4;i++)
{
fwrite(&b[i],sizeof(struct jian),1,fp3);
printf("%-10s%-5d\n",b[i].name,b[i].pay);
}
fclose(fp3);
//把b[5]写入x22.txt
}