How to write a struct to a file as a stream binary?

  • Thread starter Thread starter Roberto Rocco
  • Start date Start date
R

Roberto Rocco

Hello,

I have a struct and want to stream it to a file (binary).
I cannot use [Serializable] because this is not supported under the Compact Framework.
What is the best approach to use?
Another question in this aspect: How to I retrieve the size of my stuct? SizeOf seems not to work on structs?

Many thanks in advance,

Roberto.
 
Roberto,
I would add methods to my struct that accepted either a BinaryReader or a
BinaryWriter that read or wrote each field to the reader or writer.

Effectively implementing a simplified serialization myself.

For structs this should be fine. The problem is when you have object graphs
where objects can refer to themselves either directly or indirectly. You
need to track if a specific object has already been serialized or not.

Hope this helps
Jay

Hello,

I have a struct and want to stream it to a file (binary).
I cannot use [Serializable] because this is not supported under the Compact
Framework.
What is the best approach to use?
Another question in this aspect: How to I retrieve the size of my stuct?
SizeOf seems not to work on structs?

Many thanks in advance,

Roberto.
 
Hello Jay,

thank you for your reply.
Yes, I believe this is the only way it can be don in C# <sigh>.
I fear though, that it is much less performant than having it serialized as
a stream.
In C++ I would just have to pass a pointer to the memory block and its
length and write it in one woooosh, and I also could cast it back to the
appropriate structure when reading it again.

Well, Ill have to try it out te way you described it and keep my fingers
crossed and hope performance is not too bad :)

Roberto.
 
Roberto Rocco said:
thank you for your reply.
Yes, I believe this is the only way it can be don in C# <sigh>.
I fear though, that it is much less performant than having it serialized as
a stream.

It should perform pretty much as well - and makes the format absolutely
evident in your code. For most of the time I prefer explicit
saving/loading to serialization, although the latter certainly has its
uses.

If you're worried about performance, I suggest you actually *measure*
it rather than fearing it.
In C++ I would just have to pass a pointer to the memory block and its
length and write it in one woooosh, and I also could cast it back to the
appropriate structure when reading it again.

Only if you definitely were using the same compiler (including the same
version) to read as to write. Oh, and only if the memory block itself
doesn't contain any pointers to other things. It's basically a pretty
unsafe way of saving data, IMO.
Well, Ill have to try it out te way you described it and keep my fingers
crossed and hope performance is not too bad :)

I rarely find performance is an issue in implementation, if the
architecture is okay - and when it is proved to be an issue, that's the
time to address it.
 
Roberto,
In addition to Jon's comments.
I fear though, that it is much less performant than having it serialized as
a stream.
I would expect it would be more performant!

Serialization (both .NET & MFC) writes control information to the stream,
such as types & field names, in addition to the field data, to allow the
object to be recreated during deserialization. Here we are writing raw field
data. Also as Jon stated, we are explicit about the file structure.

This recent article discusses (& gives samples) on using BinaryReader &
BinaryWriter to pass data via sockets between a PDA & the desktop.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp

The same technique can be used with a FileStream instead of a NetworkStream.

To see the control information written during .NET Binary Serialization open
a file created with Serialization in VS.NET.

Hope this helps
Jay

Roberto Rocco said:
Hello Jay,

thank you for your reply.
Yes, I believe this is the only way it can be don in C# <sigh>.
I fear though, that it is much less performant than having it serialized as
a stream.
In C++ I would just have to pass a pointer to the memory block and its
length and write it in one woooosh, and I also could cast it back to the
appropriate structure when reading it again.

Well, Ill have to try it out te way you described it and keep my fingers
crossed and hope performance is not too bad :)

Roberto.


Jay B. Harlow said:
Roberto,
I would add methods to my struct that accepted either a BinaryReader or a
BinaryWriter that read or wrote each field to the reader or writer.

Effectively implementing a simplified serialization myself.

For structs this should be fine. The problem is when you have object graphs
where objects can refer to themselves either directly or indirectly. You
need to track if a specific object has already been serialized or not.

Hope this helps
Jay

Hello,

I have a struct and want to stream it to a file (binary).
I cannot use [Serializable] because this is not supported under the Compact
Framework.
What is the best approach to use?
Another question in this aspect: How to I retrieve the size of my stuct?
SizeOf seems not to work on structs?

Many thanks in advance,

Roberto.
 
Back
Top