YTU 2924: 文件操作--二进制文件读入

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

时间限制: 1 Sec  内存限制: 128 MB
提交: 58  解决: 20

题目描述

现有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 <iostream>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <stdlib.h>
using namespace std;
struct student
{
    char name[20];
    int num,English,Math,Chinese;
};
int main()
{
    ifstream infile("student.dic",ios::in|ios::binary);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        return -1;
    }
    int n;
    student stu;
    while((cin>>n)&&n!=0)
    {
        infile.seekg((n-1)*sizeof(stu),ios::beg);
        infile.read((char *)&stu,sizeof(stu));
        cout<<stu.name<<" "<<stu.num<<" "<<stu.English<<" "<<stu.Math<<" "<<stu.Chinese<<endl;
    }
    infile.close();
    return 0;
}

posted @ 2016-04-06 16:12  小坏蛋_千千  阅读(391)  评论(0编辑  收藏  举报