censorcpu
2018-05-02 07:42
采纳率: 33.3%
浏览 1.1k
已采纳

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;
    };
    
    点赞 评论
  • nokianasty 2018-05-02 09:11

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

    #include
    #include

    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();
    
    return 0;
    

    };

    点赞 评论

相关推荐 更多相似问题