填空题
程序通过定义学生结构体变量,存储学生的学号、姓名和三门课的成绩。所有学生数据均以二进制方式输出到student.dat文件中。函数fun的功能是:从文件中找出指定学号的学生数据,读入此学生数据,对该学生的分数进行修改,使每门课的分数加3分,修改后重写文件中学生的数据,即用该学生的新数据覆盖原数据,其他学生数据指定不变;若找不到,则不做任何操作。
请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。
注意
:部分源程序给出如下。
不得增行或删行,也不得更改程序的结构!
试题程序:
#include <stdio.h>
#define N 5
typedef struct student{
long sno;
char name[10];
float score[3];
}STU;
void fun(char *filename,long sno)
{FILE *fp;
STU n;
int i;
fp=fopen(filename,"rb+");
/********found********/
while(!feof(
1))
{fread(&n,sizeof(STU),1,fp);
/********found********/
if(n.sno
2 sno)break;
}
if(!feof(fp))
{for(i=0;i<3;i++)
n.score[i]+=3;
/********found********/
fseek(
3,-(long)sizeof(STU),SEEK_CUR);
fwrite(&n,sizeof(STU),1,fp);
}
fclose(fp);
}
main()
{STU t[N]={{10001,"MaChao",91,92,77},{10002,"CaoKai",75,60,88},{10003,"LiSi",85,70,78},{10004,"FangFang",90,82,87},{10005,"ZhangSan",95,80,88}}, ss[N];
int i,j;
FILE *fp;
fp=fopen("student.dat","wb");
fwrite(t,sizeof(STU),N,fp);
fclose(fp);
printf("/nThe original data:/n");
fp=fopen("student.dat","rb");
fread(ss,sizeof(STU),N,fp);
fclose(fp);
for(j=0;j<N;j++)
{printf("/nNo:%1d Name:%-8s Scores:",ss[j].sno,ss[j].name);
for (i=0; i<3; i++)
printf("%6.2f",ss[j].score[i]);
printf("/n");
}
fun("student.dat",10003);
fp=fopen("student.dat","rb");
fread(ss, sizeof(STU),N,fp);
fclose(fp);
printf("/nThe data after modifing:/n");
for(j=0;j<N;j++)
{printf("/nNo:%1d Name:%-8s Scores:",ss[j].sno,ss[j].name);
for(i=0;i<3;i++)
print f("%6.2f",ss[j].score[i];
printf("/n");
}
}