baidu_37263244 2017-01-05 12:03 采纳率: 0%
浏览 2911

c++怎么将一个类的对象数组从文件中读取出出来?

如题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

  • 写回答

3条回答

  • shen_wei 2017-01-12 07:45
    关注
     struct Student
    {
        int nYear;
        char czName[20];
        char czNumber[10];
        char czTel[12];
        int nAge;
        float fScore[5];
    }Student[10];
    
    
    int main()
    { 
        int nCount = 0;
        FILE *fp = fopen("22.txt","rb");
        if(fp == NULL)
        { 
            printf("can not open file!\n");
            exit(0);
        }
        for(int i = 0;i < 100;i ++)
        {
            int nRes = fscanf(fp,"%d %s %s %s %d %f %f %f %f %f",&Student[i].nYear,Student[i].czName,Student[i].czNumber,Student[i].czTel,
                &Student[i].nAge,&Student[i].fScore[0],&Student[i].fScore[1],&Student[i].fScore[2],&Student[i].fScore[3],&Student[i].fScore[4]);
            if (nRes == -1)
            {
                nCount = i;
                fclose(fp);
                break;
            }       
        }
    
        for (int i = 0;i < nCount;i ++)
        {
            printf("%d %s %s %s %d %.2f %.2f %.2f %.2f %.2f\n",Student[i].nYear,Student[i].czName,Student[i].czNumber,Student[i].czTel,
                Student[i].nAge,Student[i].fScore[0],Student[i].fScore[1],Student[i].fScore[2],Student[i].fScore[3],Student[i].fScore[4]);
        }
        fclose(fp);
        return 0;
    }
    

    结构体的数据存储。。。

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏