delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/09/10:49:15

Message-ID: <35A4D7D4.134230D7@inf.fu-berlin.de>
Date: Thu, 09 Jul 1998 16:46:45 +0200
From: Stefan Ahl <ahl AT inf DOT fu-berlin DOT de>
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: store/get objects via fstreams with DJGPP C++

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019