Home > database >  c program crashing after reading a file the second time
c program crashing after reading a file the second time

Time:09-22

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

CodePudding user response:

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.

  • Related