473,756 Members | 9,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3888
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******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP11.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********@ema il.msn.com> schrieb im
Newsbeitrag news:OO******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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********@ema il.msn.com> schrieb im
Newsbeitrag news:OO******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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.co m>
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.co m>
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******** ********@tk2msf tngp13.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********@ema il.msn.com> schrieb im
Newsbeitrag news:OO******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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******** ********@tk2msf tngp13.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********@ema il.msn.com> schrieb im
Newsbeitrag news:OO******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP11.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
4621
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 console as well as to file, my code is as below, ======================================================================= #include <iostream.h> #include<ostream> #include<sstream>
16
3498
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 buffers. Basically, I want to be able to use all the standard read and write functions, but I want them to refer to memory locations, rather than disk files. I do not want to touch the legacy code. Does any one know of a library
2
11947
by: stephen fx | last post by:
Hello all! Using C/C++ I can do this: struct MyStruct { int a; char b; }; MyStruct test;
0
285
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 this aspect: How to I retrieve the size of my stuct? SizeOf seems not to work on structs? Many thanks in advance, Roberto.
8
23906
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
789
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14055
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
24
4455
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 help out a newbie? #include <stdio.h> #include <ctype.h> #include <string.h>
1
3906
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 to properly serialize the System.Drawing.Icon structure to an XML file? The following code doesn't write the icon information to the xml file. private void CreateXmlFile(string filename) {
0
9482
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10062
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9728
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8733
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2694
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.