zoukankan      html  css  js  c++  java
  • Problem A: 文件操作二进制文件读入

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

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 1952  Solved: 524
    [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
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct student
    {
        char name[20];
        int num;
        int English;
        int Math;
        int Chinese;
    };
    int main()
    {
        struct student str[100];
        int number,i=0;
        FILE *fp;
        if((fp=fopen("student.dic","rb"))==NULL)
        {
            printf("Cannot open file!");
            exit(1);
        }
        else
        {
            while(fread(&str[i],sizeof(struct student),1,fp)!=0)//二进制读取函数
            {
                i++;
            }
            fclose(fp);
            while(scanf("%d",&number)!=EOF)
            {
                if(number==0)
                break;
                printf("%s %d %d %d %d\n",str[number-1].name,str[number-1].num,str[number-1].English,str[number-1].Math,str[number-1].Chinese);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    面向对象编程——设计模式之一
    mysql死锁——mysql之四
    Mysql基本类型(字符串类型)——mysql之二
    Mysql基本类型(五种年日期时间类型)——mysql之二
    Mysql基础教程——mysql之一
    JVM启动参数手册——JVM之八
    Thinkphp 框架2
    Thinkphp 框架
    流程(下)
    流程(上)
  • 原文地址:https://www.cnblogs.com/mjn1/p/8719270.html
Copyright © 2011-2022 走看看