0

(This is a homework project.) I am trying to write objects from class A (Contains couple of strings and ints, also a list from objects from class B ) into file. Then I have to read back these objects from the file and display their content. I am using this code:

Writing:

ofstream ofs("filestorage.bin", ios::app|ios::binary);
A d;
ofs.write((char *)&d, sizeof(d));
ofs.close();

Reading:

ifstream ifs("filestorage.bin", ios::binary);
A e(1);

while(ifs.read((char *)&e, sizeof(e))) {    
    cout<<e;
}

ifs.close();

<< is already redefined.

It writes the data into the file, then reads it back, displays everything I want but in the end I'm getting a nice fat "Access Violation " Error. I also tried to write and read simple variables into the file (like ints). That works fine; but when I try to read an object or string I get "Access Violation". The writing seams to be OK because I get no errors.

Can you tell me why this is happening and how can I fix it? If it is necessary I can post my A and B classes too. Thanks!

4
  • 2
    There's so many things that could go wrong with this... Read/search about serialization in C++, you'll find lots of interesting stuff. As for your question, the definition of A could let someone tell you why this doesn't work. Commented Nov 6, 2012 at 19:07
  • Got to check for the eof() otherwise you'll read past the end of the file Commented Nov 6, 2012 at 19:13
  • When you get the access violation, what does the callstack say? That may lead you directly to the problem. Commented Nov 6, 2012 at 19:21
  • You have operator << defined already. Use it to write to the file like you did to cout and define operator >>. Commented Nov 6, 2012 at 19:39

2 Answers 2

1

Implement the two operators << and >> for your class.

class A {
    int a;
    string s;
pubilc:
    friend ostream& operator<< (ostream& out, A& a ) {
        out << a.a << endl;
        out << a.s << endl;
    }
    friend istream& operator>> (istream& is, A& a ) {
        //check that the stream is valid and handle appropriately.
        is >> a.a; 
        is >> a.s;
    }
};

Write:

A b;
ofstream ofs("filestorage.bin", ios::app|ios::binary);
ofs << b;

Read:

fstream ifs("filestorage.bin", ios::binary);
A b;
ifs >> b;
Sign up to request clarification or add additional context in comments.

1 Comment

@user1803967 This would be easier if you did post your classes.
0

You can try just checking the stream status

while(ifs.read((char *)&e, sizeof(e)).good()){
     cout<<e;
}

2 Comments

You have to serialization the data. See Mat's comment.
No doubt it is not the greatest way to go, but he reports that it reads the object fine, prints, and crashes afterward - this might be "good enough", especially if A doesn't have any pointer members. And even for your solution, if you read in a loop - still have to check for eof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.