473,385 Members | 1,518 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,385 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 11908
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 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.