一、填空题:
程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生原来的数据,其它学生的数据不变。
请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
注意:源程序存放在考生文件夹下BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{FILE *fp;
/**********found**********/
fp = fopen(__1__, "rb+");
/**********found**********/
fseek(__2__, -(long)sizeof(STU), SEEK_END);
/**********found**********/
fwrite(&n, sizeof(STU), 1, __3__);
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}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n\n");
for (j=0; j
{printf("\nNo: %ld 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", n);
printf("\nThe data after modifing :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j
{printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}
解题答案:
/**********第一空**********/
fp = fopen(filename, "rb+");
/**********第二空**********/
fseek(fp, -(long)sizeof(STU), SEEK_END);
/**********第三空**********/
fwrite(&n, sizeof(STU), 1, fp);
2021-07-09 14:57
2021-07-08 14:39
2021-07-08 14:39
2021-07-08 14:27
2021-07-08 14:26
2021-07-08 14:25
2021-07-07 15:33
2021-07-07 15:32
2021-07-07 15:31
2021-07-07 15:08
2021-07-07 15:06
2021-07-06 14:23
2021-07-06 14:22
2021-07-06 14:21