Saul_goodman
Saul_goodman

Reputation: 141

Reading/Writing to a file in c++

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

Upvotes: 0

Views: 99

Answers (2)

user10043112
user10043112

Reputation:

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.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409472

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?

Upvotes: 1

Related Questions