Writing a structure to a file.

  • Thread starter Thread starter Mufasa
  • Start date Start date
M

Mufasa

How can I write a structure to a file? I've tried serializing a class but
that's way to big. I need it to be as small as possible. It's only three
elements - a datetime, int and a float.

TIA - Jeff.
 
How can I write a structure to a file? I've tried serializing a class but
that's way to big. I need it to be as small as possible. It's only three
elements - a datetime, int and a float.

TIA - Jeff.

Did you try serializing the struct without enclosing class?
Or iterate through struct elements and write name=value pairs to the
file yourself...
 
Jeff,

Well, you can do exactly that, but it would be a custom serialization
method based on record position, not on keyed values. It might be great for
performance, but extensibility is minimal, if non-existant.

If the format doesn't have to be readable, then convert everything to
bytes (using the ByteConverter class) and then write the bytes to the file.
 
How can I write a structure to a file? I've tried serializing a class but
that's way to big. I need it to be as small as possible. It's only three
elements - a datetime, int and a float.

Are you in complete control of this file? Is it a "binary" file (i.e., it
can contain any byte value from 0 - 255)? If so, then write them yourself.

For the DateTime, use the ToBinary() method to get a long which represents
the date. You can recreate your DateTime when reading the file by--what
else?--FromBinary()!
 
Mufasa said:
How can I write a structure to a file? I've tried serializing a class
but that's way to big. I need it to be as small as possible. It's only
three elements - a datetime, int and a float.

BinaryWriter and BinaryReader should be the most efficient
format.

If you really need to decrease size and are willing to burn
some CPU, then you can wrap them around a GZipStream instead
of just a FileStream.

Arne
 
Back
Top