2301_80334806 2023-11-28 23:47 采纳率: 100%
浏览 13
已结题

c语言用fopen_s成功打开文件之后闪退

程序功能比较复杂。主要问题是利用fopen_s打开文件且成功,但之后闪退了,后面写的程序都没运行。可以麻烦各位帮忙看看是什么问题吗


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000

struct student
{
    char id[20];
    char name[50];
    char department[50];
    float gpa;
};

int read_data_from_file(struct student* students, int* num)
{
    FILE* fp;
    errno_t err;
    err = fopen_s(&fp, "student_data.txt", "r");
    if (err!=0)
    {
        printf("文件无法打开。\n");
        system("pause");
        exit(1);
    }
    else {
        *num = 0;
        while (*num < MAX && fscanf_s(fp, "%s%s%s%f", students[*num].id, students[*num].name, students[*num].department, &students[*num].gpa, sizeof(students[*num].id), sizeof(students[*num].name), sizeof(students[*num].department), sizeof(students[*num].gpa)) == 4)
        {
            (*num)++;
        }
    }
}

void display_menu()
{
    printf("学生成绩查询系统");
    printf("1.显示全部学生信息\n");
    printf("2.按学号排序(升序/降序)\n");
    printf("3.按绩点(gpa)排序(升序/降序)\n");
    printf("4.按姓名查询\n");
    printf("5.按学院查询\n");
    printf("6.按gpa区间查询\n");
    printf("7.各学院人数统计\n");
    printf("8.EXIT\n");
    printf("Enter your choice\n");
}

void display_students(struct student students[],int num)
{
    printf("%15s%12s%32s%6s\n", "学号", "姓名", "学院", "GPA");
    for (int i = 0;i < num;i++)
    {
        printf("%15s%12s%32s%6.1f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
    }
}

void sort_by_id(struct student students[], int num, char order)
{
    for (int i = 0;i < num - 1;i++)
    {
        for (int j = 0;j < num - i - 1;j++)
        {
            if (strcmp(students[j].id, students[j + 1].id) > order || (strcmp(students[j].id, students[j + 1].id)) == 0 && strcmp(students[j].department, students[j + 1].department) > order)
            {
                struct student temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

void sort_by_gpa(struct student students[], int num, char order)
{
    for (int i = 0;i < num-1;i++)
    {
        for (int j = 0;j < num-i-1;j++)
        {
            if (students[j].gpa > students[j + 1].gpa || (students[j].gpa == students[j + 1].gpa) && (strcmp(students[j].id, students[j + 1].id) > order))
            {
                struct student temp = students[j];
                students[j] = students[j + 1];
                students[j + 1] = temp;
            }
        }
    }
}

void query_by_name(struct student students[],int num)
{
    char name[50];
    printf("请输入要查找的姓名:\n");
    for (int i = 0;i < num;i++)
    {
        if (strcmp(students[i].name, name) == 0)
        {
            printf("学号:%s\n姓名:%s\n学院:%s\nGPA:%.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
        }
    }
}
void query_by_gparange(struct student students[], int num)
{
    float gpa_start, gpa_end;
    printf("请输入要查询的gpa区间:\n");
    scanf_s("%f %f", &gpa_start, &gpa_end);
    for (int i = 0;i < num;i++)
    {
        if (students[i].gpa >= gpa_start && students[i].gpa <= gpa_end)
        {
            printf("学号:%s\n姓名:%s\n学院:%s\nGPA:%.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
        }
    }
}
void query_by_department(struct student students[], int num)
{
    char department[50];
    printf("请输入要查找的学院:\n");
    scanf_s("%s", department);
    for (int i = 0;i < num;i++)
    {
        if (strcmp(students[i].department, department) == 0)
        {
            printf("学号:%s\n姓名:%s\n学院:%s\nGPA:%.2f\n", students[i].id, students[i].name, students[i].department, students[i].gpa);
        }
    }
}
void sum_department(struct student students[], int num)
{
    int department_count[MAX] = { 0 };
    for (int i = 0;i < num;i++)
    {
        for (int j = 0;i < num;j++)
        {
            if (strcmp(students[i].department, students[j].department) == 0)
            {
                department_count[i]++;
            }
        }
    }
    for (int i = 0;i < num;i++)
    {
        printf("学院:%s人数:%d\n", students[i].department, department_count);
    }

}

int main(void)
{
    struct student students[MAX];
    int num = 0;
    read_data_from_file(students,&num);
    int select;
    char order;
    do {
        display_menu();
        scanf_s("%d", &select);
        switch (select)
        {
        case 1:
            display_students(students, num);
            break;
        case 2:
            printf("输入'a'升序排列,输入'd'降序排列:");
            scanf_s("%c", &order);
            sort_by_id(students, num, order);
            printf("\n按学号排序(in %s order):\n", order = 'a');
            display_students(students, num);
            break;
        case 3:
            printf("输入'a'升序排列,输入'd'降序排列:");
            scanf_s("%c", &order);
            sort_by_gpa(students, num, order);
            printf("\n按gpa排序(in %s order):\n", order = 'a');
            display_students(students, num);
            break;
        case 4:
            query_by_name(students, num);break;
        case 5:
            query_by_department(students, num);break;
        case 6:
            query_by_gparange(students, num);break;
        case 7:
            sum_department(students, num);break;
        case 8:printf("退出系统\n");break;
        default:printf("无效选择!请重新输入\n");break;
        }
    } while (select != 8);
    getch();
    return 0;
}

  • 写回答

9条回答 默认 最新

  • 急速光粒 2023-11-29 08:36
    关注

    文件不存在或者文件内容有误。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 12月7日
  • 已采纳回答 11月29日
  • 修改了问题 11月29日
  • 修改了问题 11月28日
  • 展开全部

悬赏问题

  • ¥20 一个python博客项目的相关图例
  • ¥15 轮廓提取也提取不到,有没有别的方法,如何解决?
  • ¥50 Js和c++如何将含有图片的excel文件上传到后台服务器
  • ¥15 光电神经网络,FPGA
  • ¥20 通过防火墙出入站阻止游戏程序联网失效
  • ¥15 鼠标是可以在QT界面上移动的,但是热拔插鼠标无法移动了同时板子上是没问题的,如何解决?
  • ¥15 iframe嵌套显示问题
  • ¥20 【UE4】别人打包好的ue4游戏我该如何在自己的ue4引擎上运行
  • ¥15 power bi发布的链接地址打不开
  • ¥15 pip list列表中有库,但是编译时就显示缺少库