473,383 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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.

Nov 15 '05 #1
8 3857
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.
Nov 15 '05 #2
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.
Nov 15 '05 #3
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 [MVP - Outlook]" <Ja********@email.msn.com> schrieb im
Newsbeitrag news:OO**************@TK2MSFTNGP09.phx.gbl...
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 15 '05 #4
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 [MVP - Outlook]" <Ja********@email.msn.com> schrieb im
Newsbeitrag news:OO**************@TK2MSFTNGP09.phx.gbl...
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.

Nov 15 '05 #5
Roberto Rocco <ro*****@rocco.de> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Roberto Rocco <ro*****@rocco.de> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
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/de...rp09182003.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" <ro*****@rocco.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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 [MVP - Outlook]" <Ja********@email.msn.com> schrieb im
Newsbeitrag news:OO**************@TK2MSFTNGP09.phx.gbl...
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 15 '05 #8
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/de...rp09182003.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" <ro*****@rocco.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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 [MVP - Outlook]" <Ja********@email.msn.com> schrieb im
Newsbeitrag news:OO**************@TK2MSFTNGP09.phx.gbl...
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

"Roberto Rocco" <ro*****@rocco.de> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl...
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.


Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
2
by: stephen fx | last post by:
Hello all! Using C/C++ I can do this: struct MyStruct { int a; char b; }; MyStruct test;
0
by: Roberto Rocco | last post by:
Hello, I have a struct and want to stream it to a file (binary). I cannot use because this is not supported under the Compact Framework. What is the best approach to use? Another question in...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.