实现学生记录文件的创建和查找基本操作

/**
*    实验题目:
*        实现学生记录文件的创建和查找基本操作
*    实验目的:
*        领会C/C++文件的基本操作及其算法设计
*    实验内容:
*        有若干个学生成绩记录,假设它们存放在结构体数组st中,
*    编写程序,完成如下功能:
*    1、将st数组中的学生记录写入到stud.dat二进制文件中
*    2、在stud.dat文件中查找并显示指定学生序号的学生记录
*    3、在stud.dat文件中查找并显示指定学生学号的学生记录
*/

#include <stdio.h>
#include <stdbool.h>

/*-------------设计学生记录类型--------------*/
typedef struct
{
    int no;                 //  学号
    char name[10];          //  姓名
    int age;                //  年龄
    char sex[3];            //  性别
    int chinese_deg;        //  语文成绩
    int math_deg;           //  数学成绩
    int english_deg;        //  英语成绩
}stud_type;

/*-------------用st数组的学生记录创建stud.dat二进制文件--------------*/
static void create_file(void)
{
    int i;
    FILE *fp;
    int n = 8;
    stud_type st[] = {
        {1, "陈华", 20, "男", 78, 99, 84},
        {5, "张明", 21, "男", 76, 89, 88},
        {8, "王英", 22, "女", 78, 79, 80},
        {3, "刘丽", 19, "女", 82, 59, 81},
        {2, "许可", 18, "女", 90, 90, 90},
        {4, "陈军", 23, "男", 88, 94, 94},
        {7, "朱军", 24, "男", 87, 99, 95},
        {6, "李鹏", 22, "男", 72, 93, 92},
    };

    fp = fopen("stud.dat", "wb");       //  mode:wb以只写方式打开或新建一个二进制文件,只允许写数据
    if(fp == NULL)
    {
        printf("\t提示:不能创建stud.dat文件\n");
        return;
    }

    for(i = 0; i < n; i++)
        fwrite(&st[i], 1, sizeof(stud_type), fp);
    fclose(fp);

    printf("   提示:文件stud.dat创建完毕\n");
}

/*-------------显示一个学生记录--------------*/
static void disp_stud(stud_type s)
{
    printf("   学号    姓名   年龄  性别  语文  数学  英语\n");
    printf("%5d%10s%6d%6s%6d%6d%6d\n", s.no, s.name, s.age, s.sex, s.chinese_deg, s.math_deg, s.english_deg);
}

/*-------------在二进制文件stud.dat中查找序号为i的学生记录s--------------*/
static bool find_by_xh(stud_type &s, int i)             //  引用类型
{
    FILE *fp;
    int ret;

    if(i <= 0)
        return false;

    fp = fopen("stud.dat", "rb");
    if(fp == NULL)
    {
        printf("\t提示:不能打开stud.dat文件\n");
        return false;
    }

    fseek(fp, (i - 1) * sizeof(stud_type), SEEK_SET);       //  定位在第i个记录之前
    ret = fread(&s, sizeof(stud_type), 1, fp);
    if(ret == 1)
    {
        fclose(fp);
        return true;                                        //  成功读取第i个记录
    }
    else
    {
        fclose(fp);
        return false;                                       //  不能读取第i个记录,返回假
    }
}

/*-------------在二进制文件stud.dat中查找学号为no的学生记录s--------------*/
static bool find_by_no(stud_type &s, int no)
{
    FILE *fp;

    fp = fopen("stud.dat", "rb");
    if(fp == NULL)
    {
        printf("\t提示:不能打开stud.dat文件\n");
        return false;
    }
    fseek(fp, 0, SEEK_SET);                                 //  定位在文件开头
    while(fread(&s, sizeof(stud_type), 1, fp) == 1)         //  从二进制文件中循环读取
    {
        if(s.no == no)                                      //  找到学号为no的记录,返回真
        {
            fclose(fp);
            return true;
        }
    }
    fclose(fp);

    return false;                                           //  没有找到学号为no的记录,返回假
}

int main(void)
{
    int i;                                                  //  序号
    int no;                                                 //  学号
    stud_type s;

    printf("操作过程如下:\n");
    printf("  (1)创建学生记录stud.dat文件\n");
    create_file();
    printf("  (2)按序号查找,输入序号:");
    scanf("%d", &i);
    if(find_by_xh(s, i))
        disp_stud(s);
    else
        printf("   >文件不能打开或者输入的记录序号错误\n");

    printf("  (3)按学号查找,输入学号:");
    scanf("%d", &no);
    if(find_by_no(s, no))
        disp_stud(s);
    else
        printf("   >文件不能打开或者输入的学号错误\n");

    return 0;
}
测试结果:

操作过程如下:
  (1)创建学生记录stud.dat文件
   提示:文件stud.dat创建完毕
  (2)按序号查找,输入序号:5
   学号    姓名   年龄  性别  语文  数学  英语
    2      许可    18    女    90    90    90
  (3)按学号查找,输入学号:3
   学号    姓名   年龄  性别  语文  数学  英语
    3      刘丽    19    女    82    59    81

猜你喜欢

转载自blog.csdn.net/xiezhi123456/article/details/87784069