467,877 Members | 1,283 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Conversion struct/class <--> array

Hi friends,
In C++, we can declare a struct and write it into file just by giving the
address the strct instance (using &).
for example:
/////////////////////////////////

typedef struct Test
{
int intV;
char charV[8];
float floatV;
};

// create a struct
Test ts;
ts.intV = 100;
strcpy(ts.charV, "12345678");
ts.floatV = 99.99;

CFile f;
....
f.Write(&ts, sizeof(TEST)); <-->
....

////////////////////////////

My question is:
in C#, could we read/write a struct/class from/to file?

how to convert a class/struct to an array?

Nov 16 '05 #1
  • viewed: 1918
Share:
3 Replies
Dean,

Unfortunately, there isn't a really easy way to do this. If you wanted,
you could call the CreateFile API through the P/Invoke layer and get the
handle to the file, and then call a version of ReadFile which has a managed
signature to take the structure into it (which it would populate on the way
back from interop).

Other than that, there really isn't a good way to do it except by
reading the bytes, and then populating the structure yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dean L. Howen" <co*****@hotpop.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
Hi friends,
In C++, we can declare a struct and write it into file just by giving the
address the strct instance (using &).
for example:
/////////////////////////////////

typedef struct Test
{
int intV;
char charV[8];
float floatV;
};

// create a struct
Test ts;
ts.intV = 100;
strcpy(ts.charV, "12345678");
ts.floatV = 99.99;

CFile f;
...
f.Write(&ts, sizeof(TEST)); <-->
...

////////////////////////////

My question is:
in C#, could we read/write a struct/class from/to file?

how to convert a class/struct to an array?

Nov 16 '05 #2
you can use a binaryformatter to do binary serialization to and from a byte
stream, but the resulting stream isn't very interop friendly because it
contains more than just the data. type information is also saved for
deserialiation I believe.

"Dean L. Howen" wrote:
Hi friends,
In C++, we can declare a struct and write it into file just by giving the
address the strct instance (using &).
for example:
/////////////////////////////////

typedef struct Test
{
int intV;
char charV[8];
float floatV;
};

// create a struct
Test ts;
ts.intV = 100;
strcpy(ts.charV, "12345678");
ts.floatV = 99.99;

CFile f;
....
f.Write(&ts, sizeof(TEST)); <-->
....

////////////////////////////

My question is:
in C#, could we read/write a struct/class from/to file?

how to convert a class/struct to an array?

Nov 16 '05 #3
Thanks so much for your replies

I think. I must define a method that Convert to byte array. After that,
passing this array to BinaryWriter. But it not a good way because of many
steps.
---------------------------


"Dean L. Howen" <co*****@hotpop.com> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
Hi friends,
In C++, we can declare a struct and write it into file just by giving the
address the strct instance (using &).
for example:
/////////////////////////////////

typedef struct Test
{
int intV;
char charV[8];
float floatV;
};

// create a struct
Test ts;
ts.intV = 100;
strcpy(ts.charV, "12345678");
ts.floatV = 99.99;

CFile f;
...
f.Write(&ts, sizeof(TEST)); <-->
...

////////////////////////////

My question is:
in C#, could we read/write a struct/class from/to file?

how to convert a class/struct to an array?

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Daniel Brown | last post: by
7 posts views Thread by Hendrik Schober | last post: by
4 posts views Thread by Mario | last post: by
3 posts views Thread by jetweedy | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.