473,326 Members | 1,972 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,326 software developers and data experts.

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
3 2176
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
Hi there, I use a Custom OPC interface. Aftre reading multiple data I get an object which includes an array. But in design time this is only declared as object (OPC definition, can not change...
5
by: Daniel Brown | last post by:
I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to marshal a structure that contains an array of structures. When executed, the following code throws an ArgumentException...
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
7
by: Hendrik Schober | last post by:
Hi, I am trying to test a command line parser. So I came up with this (simplified): struct test { int argc; const char** argv; template< int N >
4
by: Mario | last post by:
Hy, I have the fallowing code and I don't understand why is not working ... in a header file: typedef void* cheie; typedef void* valoare; struct _Tabel; typedef _Tabel* Tabel;
3
by: jetweedy | last post by:
Hi, I'm trying to figure out how I can reassign values within an array of structures. I'm trying to write a really simple text game to learn some C++, and I can't for the life of me find...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
0
by: PauloD | last post by:
public struct Character_Items { public short Armor; public short Helm; public short WeaponL; public short WeaponR; public short Pants; public short Boots; public short Gloves; public short...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.