473,770 Members | 2,144 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 11948
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
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
14
19059
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
3262
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
1790
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
5211
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
1367
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
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
10101
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...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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...
1
7456
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
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.