473,785 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read/write a struct to a System.IO.Strea m

Hello all!

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

MyStruct test;
fread(&test,siz eof(MyStruct),1 ,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(by te[] 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 11949
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*******@hotm ail.com> wrote in message
news:Ok******** *******@TK2MSFT NGP10.phx.gbl.. .
Hello all!

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

MyStruct test;
fread(&test,siz eof(MyStruct),1 ,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(by te[] 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*******@hotm ail.com> wrote in message
news:OE******** ******@TK2MSFTN GP11.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**********@s pammotel.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*******@hotm ail.com> wrote in message
news:Ok******** *******@TK2MSFT NGP10.phx.gbl.. .
Hello all!

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

MyStruct test;
fread(&test,siz eof(MyStruct),1 ,fp);

How can a do this in C#? I found a Stream class,maybe I can use
System::Read(by te[] 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
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
3500
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
14
19062
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 example, I'd like a Windows client PC (native little endian) to be able to read a network stream coming in as big endian. I want to be able to read a short for example without worrying about flipping the bytes. Thank you, Laszlo
1
3263
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 advance
3
1791
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 VB ? thanks
9
5212
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 facing problems: Assume that FILE* filePointer; unsigned char lineBuffer;
3
2164
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 where TempRecTypeis declared as
3
1368
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 device. The problem is that when I am debugging and step through the code there is a long enough pause that I recieve the entire string. If I just run it, I only get the 1st character. I have attached the code below. Public Class Form1 ...
0
2888
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 ?myResp.GetResponseStream() {System.Net.ConnectStream}
0
9491
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
10163
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
9959
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
8988
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
6744
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();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
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
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.