Mail Archives: djgpp/1998/07/09/10:49:15
Hi,
I am a beginner in daily C++ programming.
My OO experience is build on Java.
Now I try to serialize objects within a way I have read
in the book 'C++ in 21 days'.
Imagine You have an object of type person and You want
to store it on disk. You should be able to do that like this:
class person{
public:
person();
person(const char * const name, const char * const lastname);
~person();
private:
char *name = 0;
char *lastname = 0;
};
// some code
person::person() {
}
person::person(const char * const name, const char * const lastname) {
this->name = name;
this->lastname = lastname;
}
// some other code like the destructor etc.
void main(int argc, char **argv) {
// prove for correct input like filename
person p1("Siegfried","Siegel");
ofstream ofs(argv[1],ios::binary);
ofs.write((char*) &p1, sizeof p1);
ofs.close(); // flushing included
person p2();
ifstream ifs(argv[1],ios::binary);
ifs.read((char*), sizeof p2);
ifs.close;
// p2 should have the same member data now as p1
}
That is the description of the said book.
But it does not work that way. The ofstream is not able to write
an object to a file.
I need help for that problem !
Please send answers or links soon !
- Raw text -