题目描述
现有 100100100 名学生的姓名、学号、英语、数学、语文成绩,存储在一个二进制文件 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
输入
要输出行号的整数序列,以 000 作为结束标志。
输出
输出学生信息,每个学生占一行。
输入输出样例
样例输入 #1
1 3 5 0
样例输出 #1
akdh 13773 84 83 66
sfhklas 61281 87 8 31
iwehfk 92803 54 6 77
【代码】
#include<stdio.h>
#include<stdlib.h>
#include<fstream>
struct student
{
char name[20];
int num;
int score1;
int score2;
int score3;
};
int main()
{
int i = 0,n;
FILE* fp;
fp = fopen("student.dic", "rb");
if (!fp)
exit(-1);
struct student stu[101];
while (!feof(fp))
{
fread((void*)&stu[i], sizeof(struct student),1, fp);
i++;
}
fclose(fp);
while (scanf("%d", &n) && n != 0)
{
printf("%s %d %d %d %d\n", stu[n - 1].name, stu[n - 1].num, stu[n - 1].score1, stu[n - 1].score2, stu[n - 1].score3);
}
return 0;
}