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

      

  • 相关阅读:
    [Algorithms] Counting Sort
    [LeetCode] Sort Colors
    [LeetCode] Contains Duplicate III
    [LeetCode] Contains Duplicate
    [LeetCode] Two Sum II
    [LeetCode] Linked List Cycle II
    [LeetCode] Linked List Cycle
    [LeetCode] Longest Palindromic Substring
    [LeetCode] Two Sum
    [LeetCode] Rectangle Area
  • 原文地址:https://www.cnblogs.com/mjn1/p/8719270.html
Copyright © 2011-2022 走看看