文件写入 结构体 的问题 求解!
这我写的代码,但是我找不到错误,请大家帮帮忙啊,看看是怎么回事儿!#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student_type
{
char name[8];
long num,clas;
int score1,score2,score3;
double ave;
}stud[4];
int main(void)
{
long num,clas,
int i,score1,score2,score3,ave;
char name[8];
FILE *fp=fopen("score.txt","w");
FILE *fq;
if (!fp)
{
printf("file to open") ;
}
for(i=0;i<4;i++)
{
printf("请输入第%d个学生学号、姓名、班级、三门课成绩:\n",i+1);
scanf("%ld%s%ld%d%d%d",&stud[i].num,stud[i].name,&stud[i].clas,&stud[i].score1,&stud[i].score2,&stud[i].score3);
stud[i].ave=1.0*((stud[i].score1+stud[i].score2+stud[i].score3)/3);
fprintf(fp,"%ld %s %ld %d %d %d %lf\n",stud[i].num,stud[i].name,stud[i].clas,&stud[i].score1,&stud[i].score2,stud[i].score3,stud[i].ave);
printf("\n");
}
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
if((fq=fopen("score.text","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
if((fq=fopen("score.text","r"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while(! feof(fq))
{
fscanf(fq,"%ld%s%ld%d%d%d%lf",&num,name,&clas,&score1,&score2,&score3,&ave);
printf("%ld %s %ld %d %d %d %lf\n",num,name,clas,score1,score2,score3,ave);
printf("\n");
};
if(fclose(fq))
{
printf("Can not close the file!\n");
exit(0);
}
return 0;
}