C++ запись/чтение vector в/из файла

Я пытаюсь написать код который может целиком записать и прочитать вектор инициализированный пользовательским классом.

Вот что я на данный момент сделал:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

class student
{
public:
    student()
    {

    }
    student(string name, int group, float mark, int facult[5])
    {
        this->name = name;
        this->group = group;
        this->mark = mark;
        this->facult[0] = facult[0];
        this->facult[1] = facult[1];
        this->facult[2] = facult[2];
        this->facult[3] = facult[3];
        this->facult[4] = facult[4];
    }
    string name = "Not set";
    int group = 0;
    float mark = 0.0;
    int facult[5] = {0};
};

int facultatives[] = { 0,1,1,0,0 };
float mymark = 7.14;



int main()
{
    fstream fs;
    student Matvey("Matvey", 31303117, 9.7, facultatives);
    student Adam("Adam", 31303117, 8.8, facultatives);

    vector<student> ClassList;
    ClassList.push_back(Matvey);
    ClassList.push_back(Adam);
    
    
    
    fs.open("list.txt", fstream::trunc | fstream::out);
    if (!fs.is_open())
    {
        cout << "Ошибка открытия файла" << endl;
        
    }
    else
    {
        fs.write((char*)&ClassList, sizeof(ClassList));
    }

    fs.close();
    
    fs.open("list.txt", fstream::in | fstream::out);
    if (!fs.is_open())
    {
        cout << "Ошибка открытия файла " << endl;
        
    }
    else
    {
        vector<student> ClassListrd;

        fs.read((char*)&ClassListrd, sizeof(ClassListrd));

        student buff;

        buff = ClassListrd[0];
        cout << buff.name << buff.group <<endl;
        buff = ClassListrd[1];
        cout << buff.name << buff.group <<endl; 
    }
    fs.close();
}

Это вызывает ошибку вида при чтении из файла

Попадаю я в исключение. Помогите пожалуйста решить эту проблему.


Ответы (0 шт):