其他分享
首页 > 其他分享> > 学习笔记 按数据块读写文件存取学生信息

学习笔记 按数据块读写文件存取学生信息

作者:互联网

任务描述

本关任务与第2关任务相似。(按格式读写文件存取学生信息)

相关知识(略)

编程要求

本关的编程任务是补全step2/fileBin.c文件中ReadStudInfo函数和SaveResult函数,以实现按数据块使用FILE结构存取学生信息的功能。具体要求如下:

1.ReadStudInfo函数功能:实现从二进制文件中读取学生的基本信息。函数ReadStudInfo函数原型为:

void ReadStudInfo(const char *fileName, STUDENT stud[]);

其中:

2.SaveResult函数功能:实现从二进制格式文件中读取学生的基本信息。函数SaveResult函数原型为:

void SaveResult(const char *fileName, STUDENT stud[],int n,int m);

其中:

测试说明

本关的测试文件是step3/main.c,其中将会调用你在step3/fileBin.c文件中完成的ReadStudInfo函数和SaveResult函数。除此之后,step3/main.c中还对学生信息进行了处理:包括计算总分、平均分和名次。其次还实现了输入测试数据、输出测试结果、写入测试文件(student.dat)、读取测试文件(result.dat):

1.调用函数Input()将标准输入的数据读入内存,然后调用SaveStudInfo()函数将数据再写入到student.dat文本文件,以便为**ReadStudInfo()**函数从文本文件读取学生信息准备测试数据。

2.调用函数ReadResult()SaveResult()函数保存的result.dat中的学生信息读入内存,并调用Print()函数进行输出。**SaveResult()**函数需要将计算出总分、平均分和名次信息的学生信息存入result.dat

3.student.dat的示例如下:

4.result.dat的示例如下:

平台会对你编写的代码进行测试:

 测试样例

预期文件内容(二进制打开)

代码实现

//main.cpp
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define COURSE_MAXNUM 5

struct student
{
    long studentID;
    char studentName[20];
    char sex[4];
    struct {
        int year;
        int month;
        int day;
    }birthday;
    float score[COURSE_MAXNUM];
    float total;
    float average;
    int rank;
};
typedef struct student STUDENT;


extern void ReadStudInfo(const char* fileName, STUDENT stud[]);
extern void SaveResult(const char* fileName, STUDENT stud[], int n, int m);


void Input(STUDENT* stud, int n, int m);
void Print(STUDENT* stud, int n, int m);
void TotalAndAverage(STUDENT* stud, int n, int m);
void RankByTotal(STUDENT* stud, int n, int m);

void SaveStudInfo(const char* fileName, STUDENT* stud, int n, int m);
void ReadResult(const char* fileName, STUDENT* stud);

int main()
{
    int n = 0, m = 0;
    STUDENT* stud = NULL;


    (void)scanf("%d%d", &n, &m); 

    stud = (STUDENT*)malloc(n * sizeof(STUDENT));
    if (NULL == stud)
    {
        printf("%s", strerror(errno));
        return 0;
    }
    Input(stud, n, m);
    
    SaveStudInfo("student.dat", stud, n, m);
    free(stud);
    stud = NULL;
    
    
    stud = (STUDENT*)malloc(n * sizeof(STUDENT));
    if (NULL == stud)
    {
        printf("%s", strerror(errno));
        return 0;
    }
    ReadStudInfo("student.dat", stud);
    
    TotalAndAverage(stud, n, m);
   
    RankByTotal(stud, n, m);

    SaveResult("result.dat", stud, n, m);
    free(stud);
    stud = NULL;

   
    stud = (STUDENT*)malloc(n * sizeof(STUDENT));
    if (NULL == stud)
    {
        printf("%s", strerror(errno));
        return 0;
    }
    ReadResult("result.dat", stud);

    Print(stud, n, m);
    free(stud);
    stud = NULL;
    return 0;
}

void Input(STUDENT* stud, int n, int m)
{
    int i = 0;

    for (i = 0; i < n; i++)
    {
        (void)scanf("%ld", &stud[i].studentID);
        (void)scanf("%s", stud[i].studentName);
        (void)scanf("%s", stud[i].sex);
        (void)scanf("%d-%d-%d", &stud[i].birthday.year, &stud[i].birthday.month, &stud[i].birthday.day);
        for (int j = 0; j < m; j++)
            (void)scanf("%f", &stud[i].score[j]);
        stud[i].total = 0;
        stud[i].average = 0;
        stud[i].rank = 0;
    }
}
void Print(STUDENT* stud, int n, int m)
{
    printf("%8s%12s%4s%12s%10s%10s%10s%10s%10s%10s%5s\n",
        "  NO.  ", "Name", "Sex", " Birthday ", "Computer", "English", "Math", "Music", "Total", "Average", "Rank");
    for (int i = 0; i < n; i++)
    {
        printf("%8ld", stud[i].studentID);
        printf("%15s", stud[i].studentName);
        printf("%5s", stud[i].sex);
        printf("%6d-%02d-%02d", stud[i].birthday.year, stud[i].birthday.month, stud[i].birthday.day);
        for (int j = 0; j < m; j++)
        {
            printf("%10.0f", stud[i].score[j]);
        }
        printf("%10.0f", stud[i].total);
        printf("%10.0f", stud[i].average);
        printf("%5d\n", stud[i].rank);
    }
}

void TotalAndAverage(STUDENT* stud, int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        stud[i].total = 0;
        for (int j = 0; j < m; j++)
            stud[i].total += stud[i].score[j];

        stud[i].average = stud[i].total / 4;
    }
}

void RankByTotal(STUDENT* stud, int n, int m)
{

    for (int i = 0; i < n - 1; i++)
    {
        int k = i;
        for (int j = i + 1; j < n; j++)
        {
            if (stud[j].total > stud[k].total)
            {
                k = j;
            }
        }
        if (k != i)
        {
            STUDENT temp = stud[i];
            stud[i] = stud[k];
            stud[k] = temp;
        }
        stud[i].rank = i + 1;
        if (i > 0 && stud[i].total == stud[i - 1].total)
            stud[i].rank = stud[i - 1].rank;
    }
    stud[n - 1].rank = n;
    if (n - 1 > 0 && stud[n - 1].total == stud[n - 2].total)
        stud[n - 1].rank = stud[n - 2].rank;
}

void SaveStudInfo(const char* fileName, STUDENT* stud, int n, int m)
{
    FILE* fp = fopen(fileName, "w");  
    if (fp == NULL)
    {
        printf("Failure to open %s!\n", fileName);
        exit(0);
    }
    fwrite(&n, sizeof(n), 1, fp);
    fwrite(&m, sizeof(m), 1, fp);

    fwrite(stud, sizeof(STUDENT), n, fp);
    fclose(fp); 
}


void ReadResult(const char* fileName, STUDENT* stud)
{
    FILE* fp = fopen(fileName, "r");  
    if (fp == NULL)
    {
        printf("Failure to open %s!\n", fileName);
        exit(0);
    }
    int n = 0, m = 0;

    fread(&n, sizeof(n), 1, fp);

    fread(&m, sizeof(m), 1, fp);

    fread(stud, sizeof(STUDENT), n, fp);

    fclose(fp);
}
//filetxt.cpp
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#define COURSE_MAXNUM 5

struct student
{
    long studentID;
    char studentName[20];
    char sex[4];
    struct {
        int year;
        int month;
        int day;
    }birthday;
    float score[COURSE_MAXNUM];
    float total;
    float average;
    int rank;
};
typedef struct student STUDENT;

extern void ReadStudInfo(const char* fileName, STUDENT stud[]);
extern void SaveResult(const char* fileName, STUDENT stud[], int n, int m);


void ReadStudInfo(const char* fileName, STUDENT stud[])
{
    FILE* fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("Failure to open %s!\n", fileName);
        exit(0);
    }
    int n = 0, m = 0;

    fread(&n, sizeof(n), 1, fp);

    fread(&m, sizeof(m), 1, fp);

    fread(stud, sizeof(STUDENT), sizeof(n) - 1, fp);
    fclose(fp);
}


void SaveResult(const char* fileName, STUDENT stud[], int n, int m)
{
    FILE* fp = fopen(fileName, "w");
    if (fp == NULL)
    {
        printf("Failure to open %s!\n", fileName);
        exit(0);
    }
    fwrite(&n, sizeof(n), 1, fp);
    fwrite(&m, sizeof(m), 1, fp);

    fwrite(stud, sizeof(STUDENT), n, fp);
    fclose(fp);
}

标签:fp,stud,int,读写,笔记,fileName,STUDENT,存取,void
来源: https://blog.csdn.net/Zdethm/article/details/122093803