在写文件后立即读时出错,见程序
#include<stdio.h>#include<stdlib.h>
#define SIZE 1
int main()
{
struct student
{ long number;
char name[20];
float score[3];
float average;
}stu[SIZE],stu1[SIZE];
FILE *fp;
int i,j;
printf("Input the information of the students\n");
for(i=0;i<SIZE;i++)
{printf("No.%d:",i+1);
scanf("%ld%s",&stu[i].number,stu[i].name);
for(j=0;j<3;j++)
scanf("%f",&stu[i].score[j]);
stu[i].average=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3;
}
if((fp=fopen("file","wb"))==NULL)
{printf("Cannot open the file");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error");
for(i=0;i<SIZE;i++)
printf("%ld %s %4.1f %4.1f %4.1f %4.1f\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],
stu[i].score[2],stu[i].average);
fp=fopen("file","rb");
for(i=0;i<SIZE;i++)
{ fread(&stu1[i],sizeof(struct student),1,fp);
printf("%ld %s %4.1f %4.1f %4.1f %4.1f\n",stu1[i].number,stu1[i].name,stu1[i].score[0],stu1[i].score[1],
stu1[i].score[2],stu1[i].average);}
fclose(fp);
}