468,107 Members | 1,451 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

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 11479
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by oni | last post: by
3 posts views Thread by jamal | last post: by
9 posts views Thread by Adi | last post: by
3 posts views Thread by Eddieb7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.