'c++ Access violation reading location fstream read

i have write a string into a binary file with ofstream But when i read the same binary file with fstream it appear Access violation reading location error i have read some post but i still can't manage to solve my error

void activity::creAc(){
cout << "Activity ID : " << endl;
cin >> id;
cout << "Activity Title : " << endl;
cin >> title;
cout << "Activity Type : " << endl;
cin >> type;
cout << "Activity Date : " <<  endl;
cin >> acdate;
cout << "Activity Duration : " <<endl;
cin >> duration;
cout << "Activity Fees : " <<  endl;
cin >> fee;

}

void activity::showTask(){
cout << "Activity Title : " << title<<endl;
cout << "Activity Type : " << type << endl;
cout << "Activity Date : " << acdate << endl;
cout << "Activity Duration : " << duration << endl;
cout << "Activity Fees : " << fee << endl;

}

#include <iostream>
#include <string>
#include "user.h"
#include "Teacher.h"
#include "staff.h"
#include "activity.h"
#include <fstream>
#include <istream>
#include <ostream>
using namespace std;

void main(){
    cout << "1.Create" << endl;
    cout << "2.Read" << endl;
    int choicenum;
    cin >> choicenum;
    switch (choicenum){
    case 1: {
                activity obj;
                ofstream fp2;
                fp2.open("activity.dat", ios::binary | ios::app);
                obj.creAc();
                fp2.write((char*)&obj, sizeof(obj));
                fp2.close();
                break;
    }
    case 2:
    {
        activity obj2;
        ifstream fp1;
        fp1.open("activity.dat", ios::binary);
        while (fp1.read((char*)&obj2, sizeof(obj2))){
                obj2.showTask();
        }
        fp1.close();
    }
    default: exit(0);
    };

    system("pause");
}
c++


Solution 1:[1]

std::string, like many containers, is actually a wrapper around a smart pointer to some data that is dynamically allocated as needed.

Writing it to a file will store only the address at the time the file was written (plus supporting data like length, etc.)

Thus, when you read back that file, you get a pointer from a different session/machine/etc. You then try to read the location pointed to, and it doesn't belong to your program at all, hence the access violation.

You would need to statically allocate a char of known length, not anything that requires a pointer.

Solution 2:[2]

bad declaration

class activity {
    // data member is std::string.
    typedef std::string value_type;
    value_type id;
    value_type title;
    value_type type;
    value_type acdate;
    value_type duration;
    value_type fee;
public:
    void creAc();
    void showTask();
};

good declaration

class activity {
    // data member is char[64].
    typedef char value_type[64];
    value_type id;
    value_type title;
    value_type type;
    value_type acdate;
    value_type duration;
    value_type fee;
public:
    void creAc();
    void showTask();
};

You should to learn about the memory layout of c++ class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 underscore_d
Solution 2 pigeon