What it the preferred method to write a class to a file?

Chad J gamerChad at _spamIsBad_gmail.com
Sun Jul 23 14:29:12 PDT 2006


Charles D Hixson wrote:
> I'm thinking that I should build two methods, possibly
> called write and print where write would export a "binary"
> version of the class that is not intended to be human
> readable and print would export something with, e.g.,
> numbers translated into strings.
> 
> It appears as if the methods should be instance methods of
> the class.  It looks as if write should be implemented
> something like:
> void write(Stream stream)
> {  stream.writeExact(&this, this.sizeof);	}
> 
> Though probably in this version I'd want to include
> delimiters, a type id, and a length to facilitate writing
> the corresponding read routing (which would need to be a
> class method).
> 
> It this the best approach?  Also are there any suggestions
> as to a reasonable way to specify the type id, so that it
> would be the same from run to run?  Should I keep track of
> the ids myself (manually)?  If not, how would I know on an
> attempt to read which class the type id referred to?

There was a discussion about this a while ago that had some suggestions, 
at least for the binary approach:
http://www.digitalmars.com/d/archives/digitalmars/D/37739.html

Hope it helps.



More information about the Digitalmars-d-learn mailing list