Solved

Writing a whole class to a file

Posted on 2007-03-25
5
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now