473,770 Members | 2,273 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 3891
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
4622
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
3499
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
11949
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
23907
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
14056
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
4456
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
3907
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
9619
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
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...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.