绅士羊 2023-06-06 07:09 采纳率: 78.6%
浏览 38
已结题

c语言函数文件信息的读取

已经有一个学生信息文本想根据他们的总绩点计算排名,但是出错了

void all_caculate_rank(int n) {
    struct student_information st[100];
    int i,j,temp,count=0;
    FILE *fp;
    fp=fopen("学生信息.txt","r");
    if(fp==NULL)
    {
        printf("打开文件失败!\n");
        getch();
        exit(0);
    }
    fseek(fp,0L,SEEK_SET);
    for(i=0;i<n;i++){
        count = fscanf(fp,"%s\t%s\t%s\t%c\t%s\t%f %f %f %f %f %f %f %d\n",st[i].id,st[i].name,st[i].major,&st[i].graduate,st[i].birthday,&st[i].gaoshu,&st[i].lisan,&st[i].luoji,&st[i].gpa_gaoshu,&st[i].gpa_lisan,&st[i].gpa_luoji,&st[i].gpa,&st[i].rank);
        if(count != 13){ // 检查读取文件时的错误
            printf("读取文件失败!\n");
            printf("%d",count);
            getch();
            exit(0);
        }
    }
    fclose(fp);
    //计算排名并存储
    for(i=0;i<n;i++)
    {
        temp=1;//1for(j=0;j<n;j++){
            if(st[i].gpa<st[j].gpa) temp++;//如果较小,排名下降1名
        }
        st[i].rank=temp;
    }
    //更新记录
    fp=fopen("全部排名后学生信息.txt","w");
    for(i=0;i<n;i++)
    {
        fprintf(fp,"%s\t%s\t%s\t%c\t%s\t%f %f %f %f %f %f %f %d\n",st[i].id,st[i].name,st[i].major,st[i].graduate,st[i].birthday,st[i].gaoshu,st[i].lisan,st[i].luoji,st[i].gpa_gaoshu,st[i].gpa_lisan,st[i].gpa_luoji,st[i].gpa,st[i].rank);
    }
    fclose(fp);
    printf("数据重新计算成功\n");
    getch();
}

提示读取文件失败并且count 值为-1.,我的“"全部排名后学生信息”文件为空文件,学生信息文件为已有数据文件,内容如下:
20220001 leo computer N 2003-04-18 80.00 80.00 80.00 3.00 3.00 3.00 9.00 0
20220002 erin english N 2004-06-27 60.00 78.00 87.00 0.00 2.80 3.70 6.50 0
20220003 marry computer N 2004-09-23 80.00 90.00 80.00 3.00 4.00 3.00 10.00 0
20220004 bore computer N 2004-05-05 87.00 88.00 76.00 3.70 3.80 2.60 10.10 0
20220005 jerry computer N 2004-05-06 60.00 60.00 60.00 0.00 0.00 0.00 0.00 0
20220006 wade computer N 2004-09-09 50.00 50.00 50.00 0.00 0.00 0.00 0.00 0
20220007 nora english N 2003-08-07 87.00 88.00 67.00 3.70 3.80 1.70 9.20 0
20220008 hearth english N 2004-04-04 88.00 67.00 56.00 3.80 1.70 0.00 5.50 0
20220009 warmth english N 2004-06-06 87.00 68.00 88.00 3.70 1.80 3.80 9.30 0
20220010 hot english N 2003-03-03 88.00 34.00 76.00 3.80 0.00 2.60 6.40 0
20220011 cool english N 2003-01-01 88.00 77.00 66.00 3.80 2.70 1.60 8.10 0
20220012 cold computer N 2004-10-10 77.00 88.00 88.00 2.70 3.80 3.80 10.30 0
20220013 park computer N 2004-12-12 77.00 77.00 77.00 2.70 2.70 2.70 8.10 0
20220014 line computer N 2004-11-11 87.00 77.00 88.00 3.70 2.70 3.80 10.20 0
20220015 andy computer N 2004-10-10 78.00 46.00 89.00 2.80 0.00 3.90 6.70 0
20220016 joker computer N 2004-08-09 90.00 90.00 90.00 4.00 4.00 4.00 12.00 0
20220017 king computer N 2003-10-12 100.00 100.00 100.00 5.00 5.00 5.00 15.00 0
20220018 yeal computer N 2003-09-05 77.00 66.00 88.00 2.70 1.60 3.80 8.10 0
20220019 norm computer N 2003-02-02 34.00 66.00 56.00 0.00 1.60 0.00 1.60 0
俩个文件格式都为utf8

  • 写回答

3条回答 默认 最新

  • a5156520 2023-06-06 08:40
    关注

    我这里测试,除了打印成绩那里需要改进下,其他地方都没太大问题。

    需要注意的一个地方是,这个数据文件“学生信息.txt”一般是需要和代码放置在同一目录。

    测试如下:

    参考链接:

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    struct  student_information{
        
        char id[20];
        char name[20];
        char major[20];
        char graduate;
        char birthday[20];
        float gaoshu;
        float lisan;
        float luoji;
        float gpa_gaoshu;
        float gpa_lisan;
        float gpa_luoji;
        float gpa;
        int rank;
        
    };
    
    
    void all_caculate_rank(int n) {
        struct student_information st[100];
        int i,j,temp,count=0;
        FILE *fp;
        fp=fopen("学生信息.txt","r");
        if(fp==NULL)
        {
            printf("打开文件失败!\n");
            // https://blog.csdn.net/m0_65601072/article/details/124651590
            getch();
            exit(0);
        }
        fseek(fp,0L,SEEK_SET);
        for(i=0;i<n;i++){
            count = fscanf(fp,"%s\t%s\t%s\t%c\t%s\t%f %f %f %f %f %f %f %d\n",st[i].id,st[i].name,st[i].major,&st[i].graduate,
            st[i].birthday,&st[i].gaoshu,&st[i].lisan,&st[i].luoji,&st[i].gpa_gaoshu,
            &st[i].gpa_lisan,&st[i].gpa_luoji,&st[i].gpa,&st[i].rank);
            
        
            
            if(count != 13){ // 检查读取文件时的错误
                printf("读取文件失败!\n");
                printf("%d",count);
                getch();
                exit(0);
            }
        }
        fclose(fp);
        //计算排名并存储
        for(i=0;i<n;i++)
        {
            temp=1;//第1名
            for(j=0;j<n;j++){
                if(st[i].gpa<st[j].gpa) temp++;//如果较小,排名下降1名
            }
           // printf("i=%d,temp=%d\n",i,temp);
            st[i].rank=temp;
        }
        //更新记录
        fp=fopen("全部排名后学生信息.txt","w");
        for(i=0;i<n;i++)
        {
            for(int j=0;j<n;j++){
                // 这里打印成绩修改下,按照当前学生结构信息里rank来进行排序打印 
                if(st[j].rank==(i+1)){
                    fprintf(fp,"%s %s %s %c %s %.2f %.2f %.2f %.2f %.2f %.2f %.2f %d\n",st[j].id,st[j].name,st[j].major,st[j].graduate,
                    st[j].birthday,st[j].gaoshu,st[j].lisan,st[j].luoji,st[j].gpa_gaoshu,
                    st[j].gpa_lisan,st[j].gpa_luoji,st[j].gpa,st[j].rank);
                }
            }
            
        }
        fclose(fp);
        printf("数据重新计算成功\n");
        getch();
    }
    
    int main(void){
        
        all_caculate_rank(19);
        
        return 0;
    }
     
    

    学生信息.txt(测试数据文件,和代码放置在同一个目录):

    20220001 leo computer N 2003-04-18 80.00 80.00 80.00 3.00 3.00 3.00 9.00 0
    20220002 erin english N 2004-06-27 60.00 78.00 87.00 0.00 2.80 3.70 6.50 0
    20220003 marry computer N 2004-09-23 80.00 90.00 80.00 3.00 4.00 3.00 10.00 0
    20220004 bore computer N 2004-05-05 87.00 88.00 76.00 3.70 3.80 2.60 10.10 0
    20220005 jerry computer N 2004-05-06 60.00 60.00 60.00 0.00 0.00 0.00 0.00 0
    20220006 wade computer N 2004-09-09 50.00 50.00 50.00 0.00 0.00 0.00 0.00 0
    20220007 nora english N 2003-08-07 87.00 88.00 67.00 3.70 3.80 1.70 9.20 0
    20220008 hearth english    N 2004-04-04 88.00 67.00 56.00 3.80 1.70 0.00 5.50 0
    20220009 warmth english    N 2004-06-06 87.00 68.00 88.00 3.70 1.80 3.80 9.30 0
    20220010 hot english N 2003-03-03 88.00 34.00 76.00 3.80 0.00 2.60 6.40 0
    20220011 cool english N 2003-01-01 88.00 77.00 66.00 3.80 2.70 1.60 8.10 0
    20220012 cold computer N 2004-10-10 77.00 88.00 88.00 2.70 3.80 3.80 10.30 0
    20220013 park computer N 2004-12-12 77.00 77.00 77.00 2.70 2.70 2.70 8.10 0
    20220014 line computer N 2004-11-11 87.00 77.00 88.00 3.70 2.70 3.80 10.20 0
    20220015 andy computer N 2004-10-10 78.00 46.00 89.00 2.80 0.00 3.90 6.70 0
    20220016 joker computer N 2004-08-09 90.00 90.00 90.00 4.00 4.00 4.00 12.00 0
    20220017 king computer N 2003-10-12 100.00 100.00 100.00 5.00 5.00 5.00 15.00 0
    20220018 yeal computer N 2003-09-05 77.00 66.00 88.00 2.70 1.60 3.80 8.10 0
    20220019 norm computer N 2003-02-02 34.00 66.00 56.00 0.00 1.60 0.00 1.60 0
    

    img

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

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 创建了问题 6月6日

悬赏问题

  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 为什么在iis上部署网站,服务器可以访问,但是本地电脑访问不了
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
  • ¥15 ADS时域 连续相位观察方法