文件操作--二进制文件读入

Home Web Board ProblemSet Standing Status Statistics
OJ系统新功能测试中,如有问题请联系 17865569030 17865569180 17865571035 尽量不要在上课时间打电话

Problem A: 文件操作--二进制文件读入

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 3022   Solved: 906
[ Submit][ Status][ Web Board]

Description

现有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

Input

要输出行号的整数序列,以0作为结束标志。

Output

输出学生信息,每个学生占一行

Sample Input

1 3 5 0

Sample Output

akdh 13773 84 83 66
sfhklas 61281 87 8 31
iwehfk 92803 54 6 77

HINT

打开文件时不需要指定路径

[ Submit][ Status][ Web Board]

代码

#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef struct
{
    char name[20];
    int num;
    int english;
    int math;
    int chinese;
}Stu;
int main()
{
    FILE *fp;
    Stu s[100];
    int i=0,n;
    fp=fopen("student.dic","rb");
    while(!feof(fp))
    {
        fread((void *)&s[i],sizeof(Stu),1,fp);
        ++i;
    }
    fclose(fp) ;
    while(scanf("%d",&n) && n)
    {
        n-=1;
        printf("%s %d %d %d %d\n",s[n].name,s[n].num,s[n].english,s[n].math,s[n].chinese);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/80821039