Read/write structs in streams (Tango, and Strings)

Robert Kosek robert.kosek at thewickedflea.com
Mon Oct 20 16:56:48 PDT 2008


Hello again folks,

My background in file-level read/write operations comes from Delphi and 
its handling of types.  So for reading and writing strings they always 
had to be written and read in a specific way, and a length integer 
prepended to the string.  For this reason you can't have a string, 
unless limited in length, in a record (Pascal's version of struct).

Can D read and write structs with dynamically sized strings to and from 
streams?

Here's an example of a struct I'd use:
 > struct MyStruct {
 >   int val1, val2;
 >   char[] id;
 >   uint somesuchvalue;
 >   // Etc...
 > }

Can D handle writing a variably-sized struct like this to a stream 
without any other steps?  Or, should I have a "write" method of the 
struct to write it to a stream?  If so, then how do I write an array of 
these structs efficiently?  (Like a batch read, ie: 
Stream.Read(myArrayOfStructs, theirLength))

I guess this is just a general question about how Tango handles streams 
and structs when writing, as well as any D specific quirks or advantages 
there may be.

I'm thinking of making a simple archive format, because I don't like the 
source control packages out there and was thinking of making my own.

Thanks for the answers in advance!

Regards,
Robert


More information about the Digitalmars-d-learn mailing list