0

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;
}
Saul_goodman
  • 141
  • 12
  • 1
    You cannot serialize a non POD struct this way. (Your struct contains a `std::string and therefore is not POD) – Borgleader Jul 08 '18 at 14:19
  • Okay so let me try without string, then it should work fine then ? – Saul_goodman Jul 08 '18 at 14:21
  • Possible duplicate of [STATUS\_ACCESS\_VIOLATION when reading file int struct](https://stackoverflow.com/questions/9422359/status-access-violation-when-reading-file-int-struct) – Borgleader Jul 08 '18 at 14:22

2 Answers2

1

The members of std::string is really nothing more than a member variable for the length, and a member variable being a pointer to the actual string contents.

Pointers are private and unique to a specific process in all modern protected multi-tasking operating systems, no other process (not even one started from the same program) can reuse the same pointer.

When you write the RelianceMart objects, you write the pointer of the name string object to the file. As mentioned above no other process can use this pointer, and therefore can't read the file.

Furthermore when you attempt to read the raw objects, you read raw data overwriting the existing data in the constructed object, and the object won't be properly constructed anymore.

You also don't open the file in binary mode, which is wrong since you write and read raw binary data, not text.


The common solution is to use serialization, and the most common way to do it is simply to overload the "output" and "input" operators << and >>.

In the overloaded functions you simply write and read each object as text, again using the formatted << and >> operators.


Lastly, please read Why is iostream::eof inside a loop condition considered wrong?

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
0

I would use a serialization framework, you could use Google's Protocol Buffers(https://developers.google.com/protocol-buffers/). If you consider a fullblown framework overkill, you can always write your own serialization framework, I've done that, I did use the JSON-format to encode the object.