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个回答

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;
};

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;

};

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!