I am trying to reading and write objects to a file in C++, writing the object works fine, reading gives segmentation core dump. I have commented the code for writing objects to file, while writing we can uncomment that part and comment the reading part.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class RelianceMart{
    string name;
    double trolley_number;
public:
    RelianceMart(){
        name = "NA";
        trolley_number = 0;
    }
    RelianceMart(string name, double trolley_number){
        this->name = name;
        this->trolley_number = trolley_number;
    }
    void setname(string name){
        this->name = name;
    }
    string getname(){
        return name;
    }
    void settrolleynumber(double trolley_number){
        this->trolley_number = trolley_number;
    }
    double gettrolleynumber(){
        return trolley_number;
    }
};
int main(){
    string name;
    double trl_num; 
    RelianceMart mart[3];
    RelianceMart obj;
//  ofstream fout("PersistentStorage.txt");
/*
    for(int i=0;i<3;i++){
        cin>>name;
        cin>>trl_num;
        mart[i] = RelianceMart(name, trl_num);
        fout.write((char *) & mart[i], sizeof(mart[i])); 
    }
    fout.close();
*/
    ifstream fin("PersistentStorage.txt");
    while(!fin.eof()){
        fin.read((char *) & obj,sizeof(obj));
        cout<< obj.getname();
    }
    fin.close();
    return 0;
}
 
    