1

Basically, the problem is already described in the title: When starting the program first time ( meaning that the new file is created then ), it works perfectly and it doesn't crash, but when trying a second time ( meaning that the file already is there ), it crashes.

Question is: Why does it crash and how do I prevent that from happening?

Here's the code:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

class TStudent
{
public:
    string Name, Surname;
    int Age;

    TStudent(string name, string surname, int age)
    {
        Name = name;
        Surname = surname;
        Age = age;
        cout <<"\n";
    }

    string toString() const
    {
        return Name + " ; " + Surname + " ; " + to_string(Age);
    }

    int aux1 = sizeof(Name), aux2 = sizeof(Surname);
};


class TRegistru
{
public:
    string name, surname;
    int age;
    vector <TStudent> students;

    TRegistru(const TStudent &other)
    {
        this->name = other.Name;
        this->surname = other.Surname;
        this->age = other.Age;

        students.push_back(other);

    }

    void adauga(const TStudent& student)
    {
        students.push_back(student);
    }

    void salveaza(string file_name)// creating the file and storing the data
    {
        ofstream file1;
        file1.open(file_name, ios::app);
        file1.write((char*)&students, sizeof(students));
        file1.close();
        cout<<"\nFile saved and closed successfully.\n"<<endl;
    }

    void sterge()
    {
        students.clear();
    }

    void incarca(string file_name)// opening the file and reading the data
    {
        ifstream file2;
        file2.open(file_name, ios::in);
        if(!file2)
        {
            cout<<"Error in opening file..";
        }
        else
        {
            cout<<"File opened successfully.\n"<<endl;
        }
        file2.seekg(0);
        file2.read((char*)&students, sizeof(students));
    }

    void afiseaza()//printing the data
    {
        for(auto student : students)
        {
            cout << student.Name << endl ;
            cout << student.Surname << endl;
            cout << student.Age << endl;
            cout <<"\n";
        }
    }

    string toString() const
    {
        string ret{};
        for(const auto& student : students)
        {
            ret += student.toString() + "\n";
        }
        return ret;
    }


};

int main()
{
    TStudent student1("Simion", "Neculae", 21);
    TStudent student2("Elena", "Oprea", 21);
    TRegistru registru(student1);
    registru.adauga(student2);
    registru.salveaza("data.bin");// creating the file and storing the data
    registru.sterge();
    registru.incarca("data.bin");// opening the file and reading the data
    registru.afiseaza();//printing the data

    return 0;
}
Vlad
  • 91
  • 7
  • what is the message when it is crashing? – Shabbir Hussain Sep 15 '21 at 14:07
  • Process returned -1073741819 (0xC0000005) – Vlad Sep 15 '21 at 14:08
  • 1
    Have a look at this question: https://stackoverflow.com/questions/12372531/reading-and-writing-a-stdvector-into-a-file-correctly – G. Sliepen Sep 15 '21 at 14:08
  • 1
    Does this answer your question? [How to write std::string to file?](https://stackoverflow.com/questions/15388041/how-to-write-stdstring-to-file) – Botje Sep 15 '21 at 14:11
  • `file1.write((char*)&students, sizeof(students));` -- This could never work. The last parameter `sizeof(students)` indicate why this could never work. If you had 1 student, 10 students, or a million students, `sizeof(students)` would remain the same, small value. That is because `sizeof` is a *compile-time* value -- it has no idea at runtime how many students there will be. – PaulMcKenzie Sep 15 '21 at 14:30

1 Answers1

2

After reading a second time your code I realised that the way you try to read and write a vector was plain wrong:

vector <TStudent> students;
...
    ifstream file2;
    file2.open(file_name, ios::in);
    file2.seekg(0);
    file2.read((char*)&students, sizeof(students)); // WRONG!!!

A vector does store its data in a contiguous way, but the address of the vector is not the address of the data. The correct way is to serialize each element of the vector to the file and then deserialize each element and push that into the vector.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252