zoukankan      html  css  js  c++  java
  • 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;
    }
    

  • 相关阅读:
    6.1.1.1 属性类型之数据属性
    6.1 理解对象
    5.7.2.4 random() 方法
    5.7.2.3 舍入方法
    5.7.2.2 min()和max()方法
    5.7.2.1 Math对象
    5.7.1.4 window对象
    frontend2_s9_part2_fe_d48_form
    s10_part3_django_basic.md
    frontend1_s9_part2_d47_html
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989391.html
Copyright © 2011-2022 走看看