censorcpu 2018-05-02 07:42 采纳率: 0%
浏览 1184
已采纳

c++ io流输入输出问题求解

构建一个结构体struct student,包括两个成员:id和avescore。
构建一个struct student类型的动态数组(new 操作),元素个数由用户输入(要求3个)。
输入数据存入数组并显示出来({1,5;2,8;3,10})
使用文本模式通过I/O流把数组内容存入磁盘文件:student.txt
从student.txt中读出数据,采用二进制模式存入文件student.bin
使用VS打开两个文件,比较两个文件的区别

  • 写回答

2条回答 默认 最新

  • nokianasty 2018-05-02 09:17
    关注

    C++ 代码已经实现,代码调试通过。请速度采用,急用C币下载!!

    
    
     #include<iostream>
    #include<fstream>
    
    using namespace std;
    struct student
    {
        int id;
        int avescore;
    };
    
    
    int main()
    {
        int numbers = 0;
        cout << "请输入元素个数:";
        cin >> numbers;
        student*  pArrStudent = new student[numbers];
        for (int i = 0; i < numbers; i++)
        {
            cout << "请输入第" << i + 1 << "个元素id和avescore,空格分隔:";
            int id = 0;
            int avescore = 0;
            cin >> id >> avescore;
            pArrStudent[i].id = id;
            pArrStudent[i].avescore = avescore;
    
        }
    
    
        //显示输入的数据
        cout << "({";
        for (int i = 0; i < numbers; i++)
        {
            if (i < numbers - 1)
            {
                cout << pArrStudent[i].id << "," << pArrStudent[i].avescore << ";";
            }
            else
            {
                cout << pArrStudent[i].id << "," << pArrStudent[i].avescore;
            }
    
        };
        cout << "})";
    
    
        //存入磁盘文件:student.txt
        ofstream oFile(L"student.txt");
        oFile.write((char*)pArrStudent, sizeof(student)*numbers);
        oFile.close();
    
    
        //从student.txt中读出数据
        student*  pArrStudentTemp = new student[numbers];
        ifstream iFileTemp(L"student.txt");
        iFileTemp.read((char*)pArrStudentTemp, sizeof(student)*numbers);
        iFileTemp.close();
    
        //采用二进制模式存入文件student.bin
        ofstream oFileTemp(L"student.bin", ios::binary);
        oFileTemp.write((char*)pArrStudent, sizeof(student)*numbers);
        oFileTemp.close();
    
        delete []pArrStudent;
        delete[]pArrStudentTemp;
    
        return 0;
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?