使用C#从二进制文件读取C ++结构 [英] reading a C++ structure from a binary file using C#

查看:75
本文介绍了使用C#从二进制文件读取C ++结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用C ++创建的二进制文件,其中包含

结构的对象:


struct student

{

int roll_no;

char name [20];

char qualified [50];

};


以下代码用于创建和写入文件:


void WriteToFile()

{< br $> b $ b学生;

s.roll_no = 1;

strcpy(s.name," nand");

strcpy(s.qualification," MCA");


CString fileName =" c:\\testdata.dat";


CFile文件;

file.Open(fileName,CFile :: modeCreate | CFile :: modeWrite |

CFile :: typeBinary);

file.Write(& s,sizeof(s));

}


现在,我想读取这个二进制文件的内容(testdata.dat)使用

C#程序并将其存储在C#结构中。任何人都可以建议我如何做到这一点。示例代码将受到高度赞赏。


谢谢


-

Nand Kishore Gupta

解决方案



" Nicholas Paldino [。NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:英国************** @ TK2MSFTNGP02.phx.gbl ...


Nand,


为了在.NET中执行此操作,我将从74字节的文件中读取

块(4 + 20 + 50)。然后,我会在
上调用静态ToInt32方法



C ++代码使用sizeof(s)...你必须从C ++中打印出这个值。它
不太可能是74,因为结构包含一个int成员,

更喜欢4字节对齐...所以我认为两个字节的填充是由

C ++编译器和记录间距将是76个字节。但检查一下,因为

它使用了pragma pack几个不同的值。


BitConverter类获取roll_no字段。然后你可以使用编码

类上的静态ASCII属性返回的

编码实例来获得一个可以用来转换重新编码的编码70

字节到字符串中(通过调用GetString方法)。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse。 com


" Nand Kishore Gupta" < Na ************** @ discussion.microsoft.com写在

消息新闻:A0 ************** ******************** @ microsof t.com ...


>>我有使用C ++创建的二进制文件,其中包含
结构的对象:

struct student
{
int roll_no;
char name [20];
char资格[50];
};

以下代码用于创建和写入文件:

void WriteToFile()
{
学生s;
s.roll_no = 1;
strcpy(s.name," nand");
strcpy(s.qualification," MCA" ;);

CString fileName =" c:\\testdata.dat" ;;

CFile文件;
file.Open(fileName,CFile: :modeCreate | CFile :: modeWrite |
CFile :: typeBinary);
file.Write(& s,sizeof(s));
}

现在,我想使用
一个C#程序读取这个二进制文件(testdata.dat)的内容并将其存储在C#structu中回覆。任何人都可以建议我如何做到这一点。示例代码将受到高度赞赏。

非常感谢

- Nand Kishore Gupta




Nand,


为了在.NET中执行此操作,我将从74字节的文件中读取

块(4 + 20 + 50)。然后,我将在

BitConverter类上调用静态ToInt32方法来获取roll_no字段。然后你可以使用Encoding类上的静态ASCII属性返回的Encoding

实例来获得一个编码,你可以用它将70个字节的重新转换为字符串

(通过调用GetString方法)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Nand Kishore Gupta" < Na ************** @ discussion.microsoft.com写在

消息新闻:A0 ************** ******************** @ microsof t.com ...


>我有一个二进制文件使用C ++创建的文件,其中包含

结构的对象:


struct student

{

int roll_no;

char name [20];

char qualified [50];

};


以下代码用于创建和写入文件:


void WriteToFile()

{

学生;

s.roll_no = 1;

strcpy(s.name," nand");

strcpy(s.qualification ,MCA);


CString fileName =" c:\\testdata.dat" ;;


CFile文件;

file.Open(fileName,CFile :: modeCreate | CFile :: modeWrite |

CFile :: typeBinary);

file.Write( & s,sizeof(s));

}


现在,我想阅读这个二进制文件(testdata.dat)的内容,使用

a

C#程序并将其存储在C#结构中。任何人都可以建议我这样做是多少

。示例代码将受到高度赞赏。


谢谢


-

Nand Kishore Gupta



" Ben Voigt" < rb*@nospam.nospamwrote in message

news:eU *************** @ TK2MSFTNGP05.phx.gbl ...
< blockquote class =post_quotes>
>

" Nicholas Paldino [。NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在消息中

news:uk ************** @ TK2MSFTNGP02.phx.gbl ...


> Nand,

为了在.NET中执行此操作,我将从74字节块中读取文件(4 + 20 +
50)。然后,我会在
上调用静态ToInt32方法



C ++代码使用sizeof(s)...你必须从C ++中打印出这个值。由于结构包含一个更喜欢4字节对齐的int成员,所以不太可能
为74 ...所以我认为
认为填充了两个字节的填充C ++编译器和记录间距将是76

字节。但检查一下,因为它使用了pragma pack几个不同的值将是

可能。



为什么? int是struct的第一个字段,第二个是char数组,没有

对齐限制,因此,无论打包,写在磁盘上的长度都是74.


威利。


I have a binary file created using C++ that contains an object of the
structure:

struct student
{
int roll_no;
char name[20];
char qualification[50];
};

the following code is used to create and write to the file:

void WriteToFile()
{
student s;
s.roll_no=1;
strcpy(s.name,"nand");
strcpy(s.qualification,"MCA");

CString fileName = "c:\\testdata.dat";

CFile file;
file.Open(fileName,CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary);
file.Write(&s,sizeof(s));
}

Now, I want to read the contents of this binary file (testdata.dat) using a
C# program and store that in a C# structure. Can any body suggest me how to
do this. Sample code would be highly appreciated.

Thanks

--
Nand Kishore Gupta

解决方案


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uk**************@TK2MSFTNGP02.phx.gbl...

Nand,

In order to do this in .NET, I would read from the file in 74 byte
blocks (4 + 20 + 50). Then, I would call the static ToInt32 method on the

The C++ code used sizeof(s)... you must print this value out from C++. It
is unlikely to be 74, since the structure contains an int member which
prefers 4-byte alignment... so I think two bytes of padding is added by the
C++ compiler and the record spacing will be 76 bytes. But check it, because
it pragma pack was used several different values would be possible.

BitConverter class to get the roll_no field. Then you can use the
Encoding instance returned by the static ASCII property on the Encoding
class to get an encoding which you can use to convert the remaning 70
bytes into strings (by calling the GetString method).

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

"Nand Kishore Gupta" <Na**************@discussions.microsoft.comwrote in
message news:A0**********************************@microsof t.com...

>>I have a binary file created using C++ that contains an object of the
structure:

struct student
{
int roll_no;
char name[20];
char qualification[50];
};

the following code is used to create and write to the file:

void WriteToFile()
{
student s;
s.roll_no=1;
strcpy(s.name,"nand");
strcpy(s.qualification,"MCA");

CString fileName = "c:\\testdata.dat";

CFile file;
file.Open(fileName,CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary);
file.Write(&s,sizeof(s));
}

Now, I want to read the contents of this binary file (testdata.dat) using
a
C# program and store that in a C# structure. Can any body suggest me how
to
do this. Sample code would be highly appreciated.

Thanks

--
Nand Kishore Gupta




Nand,

In order to do this in .NET, I would read from the file in 74 byte
blocks (4 + 20 + 50). Then, I would call the static ToInt32 method on the
BitConverter class to get the roll_no field. Then you can use the Encoding
instance returned by the static ASCII property on the Encoding class to get
an encoding which you can use to convert the remaning 70 bytes into strings
(by calling the GetString method).

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

"Nand Kishore Gupta" <Na**************@discussions.microsoft.comwrote in
message news:A0**********************************@microsof t.com...

>I have a binary file created using C++ that contains an object of the
structure:

struct student
{
int roll_no;
char name[20];
char qualification[50];
};

the following code is used to create and write to the file:

void WriteToFile()
{
student s;
s.roll_no=1;
strcpy(s.name,"nand");
strcpy(s.qualification,"MCA");

CString fileName = "c:\\testdata.dat";

CFile file;
file.Open(fileName,CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary);
file.Write(&s,sizeof(s));
}

Now, I want to read the contents of this binary file (testdata.dat) using
a
C# program and store that in a C# structure. Can any body suggest me how
to
do this. Sample code would be highly appreciated.

Thanks

--
Nand Kishore Gupta



"Ben Voigt" <rb*@nospam.nospamwrote in message
news:eU***************@TK2MSFTNGP05.phx.gbl...

>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in message
news:uk**************@TK2MSFTNGP02.phx.gbl...

>Nand,

In order to do this in .NET, I would read from the file in 74 byte blocks (4 + 20 +
50). Then, I would call the static ToInt32 method on the


The C++ code used sizeof(s)... you must print this value out from C++. It is unlikely to
be 74, since the structure contains an int member which prefers 4-byte alignment... so I
think two bytes of padding is added by the C++ compiler and the record spacing will be 76
bytes. But check it, because it pragma pack was used several different values would be
possible.

Why? The int is the first field of the struct, the second is a char array which has no
alignment restriction, so, whatever the packing, the length written on disk will be 74.

Willy.


这篇关于使用C#从二进制文件读取C ++结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆