0
点赞
收藏
分享

微信扫一扫

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

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]


한국어 中文 فارسی English ไทย
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Help Maunal

代码

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


举报

相关推荐

0 条评论