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;
}