题目描述
现有100名学生的姓名(name)、学号(num)、英语(English)、数学(Math)、语文(Chinese)成绩存储在一个二进制文件student.dic中(姓名用char[20],学号和各科成绩用int存储),现要求将指定行数的学生信息输出,每条信息占一行。
前5行学生信息为:
akdh 13773 84 83 66
fjka 30257 15 14 88
sfhklas 61281 87 8 31
hfu 38635 55 50 60
iwehfk 92803 54 6 77
输入
要输出行号的整数序列,以0作为结束标志。
输出
输出学生信息,每个学生占一行
样例输入
1 3 5 0
样例输出
akdh 13773 84 83 66
sfhklas 61281 87 8 31
iwehfk 92803 54 6 77
参考代码
#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int num;
int eng;
int math;
int chi;
}
;
int main()
{
FILE *fp;
int i,n;
struct student stu[100];
if((fp=fopen("student.dic","rb"))==NULL)
{
printf("cannot open file");
exit(1);
}
fread((void*)&stu,sizeof(struct student),100,fp);
while(scanf("%d",&n)&&n!=0)
{
printf("%s %d %d %d %dn",stu[n-1].name,stu[n-1].num,stu[n-1].eng,stu[n-1].math,stu[n-1].chi);
}
return 0;
}
解析
暂无