Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing a whole class to a file

Posted on 2007-03-25
5
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 240 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 260 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

688 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