473,287 Members | 1,629 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,287 software developers and data experts.

How to read/write a struct to a System.IO.Stream

Hello all!

Using C/C++ I can do this:
struct MyStruct
{
int a;
char b;
};

MyStruct test;
fread(&test,sizeof(MyStruct),1,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(byte[] buffer,int offset,int count)? I can get sizeof a struct,
but how to convert a struct object to byte[]??

thanks.
Nov 15 '05 #1
2 11897
You probably want to look at Serialization.

Mark the struct with the [Serializable()] attribute, then use the
BinaryFormatter or the SoapFormatter class to serialize/deserialize it
to/from the stream.

Greg

"stephen fx" <st*******@hotmail.com> wrote in message
news:Ok***************@TK2MSFTNGP10.phx.gbl...
Hello all!

Using C/C++ I can do this:
struct MyStruct
{
int a;
char b;
};

MyStruct test;
fread(&test,sizeof(MyStruct),1,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(byte[] buffer,int offset,int count)? I can get sizeof a struct, but how to convert a struct object to byte[]??

thanks.

Nov 15 '05 #2
If you're specifically talking about a bitmap or other picture, you should
use the existing System.Drawing.Bitmap class, but if you're just talking
about an arbitrary class from C++, you should use SOAP and the
SoapFormatter, that is what it is for. Soap is a special XML format for
describing objects, it is paintext readable, and therefore supposed to be
fairly easy to make from other programs.

If you mean something more complicated, you can make your class implement
the ISerializable interface (it still needs to have the [Serializable()]
attribute) and then you can write your own custom Serialize() and
Deserialize() methods.

I'm not sure, but I do believe that this may not be appropriate for writing
files with predetermined formats (such as a bitmap) as the serialised file
may contain overhead (that you can't control), which tells the deserialiser
the class type etc... but as I said, I'm not sure about that as I haven't
really looked. In which case, you'd probably just want to have Read(stream)
and Write(stream) methods in your class for doing that.

Hope this helps you :)
Greg
"stephen fx" <st*******@hotmail.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...
Yes,I think I should use Serialize.But how can I deal with the data
generated by other C/C++ program? For example,How can I write a .BMP file
reader in C#?

Than you for your help!
"Greg Bacchus" <FB**********@spammotel.com> дÈëÏûÏ¢ÐÂÎÅ
:uR**************@TK2MSFTNGP12.phx.gbl...
You probably want to look at Serialization.

Mark the struct with the [Serializable()] attribute, then use the
BinaryFormatter or the SoapFormatter class to serialize/deserialize it
to/from the stream.

Greg

"stephen fx" <st*******@hotmail.com> wrote in message
news:Ok***************@TK2MSFTNGP10.phx.gbl...
Hello all!

Using C/C++ I can do this:
struct MyStruct
{
int a;
char b;
};

MyStruct test;
fread(&test,sizeof(MyStruct),1,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(byte[] buffer,int offset,int count)? I can get sizeof a

struct,
but how to convert a struct object to byte[]??

thanks.



Nov 15 '05 #3

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...
14
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
1
by: oni | last post by:
hi all, the program should be able to log on to a pop mail account and need to just read its headers and display. any help in this regard highly apperecialted. code pref VB.NET thankz in...
3
by: jamal | last post by:
HI guys I want to make a program that will read my external pop3 e-mail account and save them to an access/sql file on my workstation. what you guys think ? Can I make something like this with...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
3
by: Eddieb7 | last post by:
HI, I come from a dephi background and i am in the process of moving to c#. In Delphi I can declare a file as being of a recordrtpe (or Struct in c#). i.e FileName = File of TempRecType ...
3
by: markclinn | last post by:
I have a device in the field that I access by the stream method. I open the Stream and do the following: 1. stream.write a character to the device. 2. stream.read the information from the...
0
by: vishnu | last post by:
Hi, Am trying to post the data over https and am getting error in httpwebresponse.getResponseStream.Please help me to get rid of this issue. Here is the message from immediate window ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.