home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cimshop!davidm
- From: davidm@consilium.com (David S. Masterson)
- Newsgroups: comp.lang.c++
- Subject: Re: File I/O using C++ (write/read objects...)
- Message-ID: <DAVIDM.92Aug26132938@consilium.com>
- Date: 26 Aug 92 20:29:38 GMT
- References: <1992Aug26.023255.10785@gucis.cit.gu.edu.au>
- Sender: root@cimshop.UUCP
- Organization: Consilium Inc., Mountain View, California
- Lines: 49
- In-reply-to: jetherid@gucis.cit.gu.edu.au's message of 26 Aug 92 02:32:55 GMT
- X-Posting-Software: GNUS 3.13 [ NNTP-based News Reader for GNU Emacs ]
-
- >>>>> On 26 Aug 92 02:32:55 GMT, jetherid@gucis.cit.gu.edu.au (Jason
- >>>>> Etheridge) said:
-
- > I am having real problems reading and writing objects to a file. A text
- > I have uses
-
- > out.write((unsigned char*) &object, sizeof(OBJECT))
-
- > where out is an fstream that allows writing to the file it is open for,
- > and object is an object of class OBJECT.
-
- > It writes the object, seemingly with no problems, but when I try and read
- > it back in the resulting object has been garbled, it's attributes changed
- > with the occasional garbage character.
-
- > Strangely enough, if I try this with an object with no functions, it
- > works perfectly.
-
- Offhand, I would guess that your object has pointers in it (possibly virtual
- pointers). Pointers, no matter how you look at them, are memory references
- and those references are not persistent (they change from invocation to
- invocation). Also, in doing the "block write" that your doing above, you are
- not saving anything that the object points to.
-
- As far as I've seen, there is no automatic, reliable approach to writing a C++
- object to a file (or sending it in a message). It requires knowing what the
- format of the object internally looks like and, possibly, converting this
- internal format into a format that is persistent.
-
- The only generic, portable approach to doing this that I've seen is what I'll
- call "linearization" of an object. That is, when object wants to write itself
- onto a stream (as defined by operator>>), it converts itself into a stream of
- bytes that will be recognizable on the other end of the stream (so that
- operator<< will understand it). With these operators defined on every object,
- newly derived objects simply worry about their internal structure and call the
- appropriate superclass method to handle the rest. The drawback with this is
- that it suggests a Smalltalk approach to creating objects (everything derives
- from the central controlling Object). Naturally, there are a lot of little
- "gotchas" in this that must be worked out (like peeking into a stream and
- making a guess about what the next object is that's coming out of the stream
- so that the appropriate object may be constructed).
-
- Comments?
- --
- ====================================================================
- David Masterson Consilium, Inc.
- (415) 691-6311 640 Clyde Ct.
- uunet!cimshop!davidm Mtn. View, CA 94043
- ====================================================================
-