Writing a structure to a file.

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.
 
G

GArlington

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...
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jeff Johnson

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()!
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top