Solved

Writing a whole class to a file

Posted on 2007-03-25
5
205 Views
Last Modified: 2012-05-05
Hello,

I am trying to output a whole class to a file using fstream objects and everything works fine. The only problem is when the class has a member of type vector: I output the class to a file successfully, then I read it back from the file and print out every member of the class to see if it read everything ok, and it outputs everything correctly, EVEN the elements of the vector, but when the program reaches the end of its code and tries to exit, I hear the sound of an error but I don't see a message window saying what the error is and even when I debug the code it doesn't send me to a breakpoint. It just hangs there doing nothing. This is my code

#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

class simple{
public:
      int a;
      int b;
      char* name;
      vector<int> vec;

      simple(){};
      virtual ~simple(){vec.clear();}
};

void saveTo(const char *fileName, simple *obj)
{

      ofstream fout(fileName, ios::binary);
      fout.write((char *)(obj), sizeof(*obj));
      fout.close();

}

void readFrom(const char* fileName, simple *obj)
{
      ifstream fin(fileName, ios::binary);
      fin.read((char *)(obj), sizeof(*obj));
      fin.close();
}

void main()
{
      simple *s = new simple;
      s->a = 1;
      s->b = 2;
      s->name = "something";
      s->vec.push_back(5);
      s->vec.push_back(6);

      simple *si = new simple;

      saveTo("file.dat", s);
      readFrom("file.dat", si);
      cout << si->a << "\n" << si->b <<  "\n" << si->name << "\n";
      for (int i=0; i<si->vec.size(); i++)
      {
            cout << si->vec[i] << "\n";
      }
      delete s;
      delete si;
}

Any ideas why this is happening?
0
Comment
Question by:silethunde
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18790152
You are saving the char* and not the name "something".  You need to use a char array and not just a pointer.  yoo then need to "serialize" you class and save each element separately, making sure you output the array and not just the pointer.
0
 
LVL 3

Assisted Solution

by:Darrylsh
Darrylsh earned 60 total points
ID: 18790187
One more thing to note, your program seems to be working because the write and read is in the same program and so just happens to be pointing at the same thing, but if instead you had  a separate read and write program you would clearly see it does not work properly.
0
 

Author Comment

by:silethunde
ID: 18790203
When I don't have vectors in the class everything works fine. I don't think the problem is the char* name I have there. Also as I said when I read the values back from the file into a new class and I output the results on screen, everything is fine again. The only problem is the vector there.
0
 

Author Comment

by:silethunde
ID: 18790221
And I guess you might be right about the program working when read and write is in the same program, but why does the vector cause the program to crush?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 65 total points
ID: 18792582
>>>> I don't think the problem is the char* name
You are wrong. You can't store pointers to a file and expect that these are valid when you read thenm back. Same happens with std::vector. It internally has a pointer to the array what means when you store std::vector you didn't store the int values but only a pointer. All that can work as long as you make storing and reading back in one function cause the pointers are still valid. If you create a new object for restore it will get the same pointers from the file that already where owned (and deleted at end) by an other vector instance what makes it crash.

What to do:

You can use fixed size arrays both for char strings and int array. Then your binary store would write the values rather than pointers and it works.

Or do what Darryish told you: serialize all members - means store size and values in case of vector and store the string contents including the terminating zero char in case of char* - and read back same way. Unfortunately that can't be done with one write/read but member for member only.

Regards, Alex
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question