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

Message-ID: <35A4E9BD.E9486B11@ipass.net>
From: Terry <iceman AT ipass DOT net>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: store/get objects via fstreams with DJGPP C++
References: <35A4D7D4 DOT 134230D7 AT inf DOT fu-berlin DOT de>
Lines: 93
Date: Thu, 09 Jul 1998 16:08:46 GMT
NNTP-Posting-Host: ts3-130-ppp.ipass.net
NNTP-Posting-Date: Thu, 09 Jul 1998 12:08:46 EDT
Organization: iPass.Net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Stefan Ahl wrote:
> 
> 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.

Hmm, this only saves the value of the pointers to disk.  If you
were to use another program to read the object, then who knows
what the pointers will be pointing to.

I would use something like this:

class person{
public:
    person();
    person(const char * const name, const char * const lastname);
    ~person();
    void savePerson(ofstream&);
    void readPerson(ifstream&);
private:
    char *name = 0;
    char *lastname = 0;
};

void person:savePerson(ofstream& outf)
{
   int n = strlen(name);
   outf.write((char *) &n,sizeof(int));
   outf.write(name,n);
   n = strlen(lastname);
   outf.write((char *) &n,sizeof(int));
   outf.write(lastname,n);
}

void person:readPerson(ifstream& inf)
{
   int n;
   inf.read((char *) &n,sizeof(int));
   name = new char[n+1];
   inf.read(name,n);
   name[n] = 0; // put NULL at end of string
   inf.read((char *) &n,sizeof(int));
   lastname = new char[n+1];
   inf.read(lastname,n);
   lastname[n] = 0;  // NULL at end of string
}

Now, I haven't programmed in C++ for a while so the syntax might
be a little off.  However, the general idea is there.

Hope this helps.

Terry

- Raw text -


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