如何读取和写入一个结构文件使用C#?

时间:2021-10-09 11:08:05

I can use write(&stName,sizeof(stName),&FileName) and define a same struct in other program to read the file(XXX.h) when i use C, But I want do the same use C# and I should not use the unsafe mode. How do to solve the problem?

我可以使用write(&stName,sizeof(stName),&FileName)并在其他程序中定义一个相同的结构来读取文件(XXX.h),当我使用C时,但是我想做同样的使用C#而我不应该使用不安全的模式。怎么解决这个问题?

Edit:

thanks all. I will to try them

谢谢大家。我会尝试一下

Edit:

Now if I want to use C write the Struct to file.h and use C# to read the struct from file.h, may I have chance solve that and not to count the offset? Because count the offset is not a good answer when I want to add some variable or other struct in the struct.

现在,如果我想使用C将struct写入file.h并使用C#从file.h读取结构,我可以解决这个问题,而不是计算偏移量吗?因为当我想在结构中添加一些变量或其他结构时,计算偏移量不是一个好的答案。

7 个解决方案

#1


Look at the ISerializable interface and Serialization in general.

一般来说,请查看ISerializable接口和序列化。

#2


Even in C, this is a dangerous thing to do IMO. If you use a different compiler, operating system, architecture etc you can very easily "break" your data files - you're absolutely relying on the layout of the data in memory. It's a bit like exposing fields directly instead of properties - the in-memory layout should be an implementation detail which can be changed without the public form (the file) changing.

即使在C中,这对IMO来说也是一件危险的事情。如果您使用不同的编译器,操作系统,体系结构等,您可以非常轻松地“破坏”您的数据文件 - 您绝对依赖于内存中数据的布局。这有点像直接暴露字段而不是属性 - 内存中布局应该是一个实现细节,可以在不改变公共表单(文件)的情况下进行更改。

There are lots of ways of storing data, of course. For example:

当然,有很多种方法可以存储数据。例如:

  • Binary serialization (still pretty fragile, IMO)
  • 二进制序列化(仍然非常脆弱,IMO)

  • XML serialization
  • Google's Protocol Buffers
  • 谷歌的协议缓冲区

  • Thrift
  • YAML
  • Hand-written serialization/deserialization e.g. using BinaryReader and BinaryWriter
  • 手写的序列化/反序列化,例如使用BinaryReader和BinaryWriter

There are balances in terms of whether the file is human readable, speed, size etc. See this question for answers to a similar question about Java.

在文件是否可读,速度,大小等方面存在余额。请参阅此问题以获取有关Java的类似问题的答案。

#3


You should take a look at the NetDataContractSerializer. You can markup those portions of the struct that you wish to serializer and use a file stream to write them out.

你应该看一下NetDataContractSerializer。您可以标记希望序列化的结构部分,并使用文件流将其写出。

#4


Look at the StructLayoutAttribute

查看StructLayoutAttribute

#5


Use Managed C++ or C++/CLI. It can read your .h file struct. It can read and write using:

使用托管C ++或C ++ / CLI。它可以读取你的.h文件结构。它可以使用以下方式读写:

read(in, &structure, sizeof(structure));
write(out, &structure, sizeof(structure));

and it can transfer that data very simply to anything else in .NET.

它可以非常简单地将数据传输到.NET中的任何其他内容。

#6


You'll have to convert each member of the struct individually using Bitconverter.convert(). This works well when your struct holds numeric data types, but you might have to do something more complex when using structs that contain more complicated data types like strings, arrays, and collections. For purposes like this, you will want to check out the .Net serialization facilities that other have mentioned.

您必须使用Bitconverter.convert()单独转换结构的每个成员。当struct包含数值数据类型时,这很有效,但在使用包含更复杂数据类型(如字符串,数组和集合)的结构时,可能必须执行更复杂的操作。出于这样的目的,您需要查看其他人提到的.Net序列化工具。

#7


You can look into Google Protocol buffers as well. You may not want to add another dependency into your code, but it's meant to allow you to do this sort of thing.

您也可以查看Google协议缓冲区。您可能不希望在代码中添加另一个依赖项,但它意味着允许您执行此类操作。

#1


Look at the ISerializable interface and Serialization in general.

一般来说,请查看ISerializable接口和序列化。

#2


Even in C, this is a dangerous thing to do IMO. If you use a different compiler, operating system, architecture etc you can very easily "break" your data files - you're absolutely relying on the layout of the data in memory. It's a bit like exposing fields directly instead of properties - the in-memory layout should be an implementation detail which can be changed without the public form (the file) changing.

即使在C中,这对IMO来说也是一件危险的事情。如果您使用不同的编译器,操作系统,体系结构等,您可以非常轻松地“破坏”您的数据文件 - 您绝对依赖于内存中数据的布局。这有点像直接暴露字段而不是属性 - 内存中布局应该是一个实现细节,可以在不改变公共表单(文件)的情况下进行更改。

There are lots of ways of storing data, of course. For example:

当然,有很多种方法可以存储数据。例如:

  • Binary serialization (still pretty fragile, IMO)
  • 二进制序列化(仍然非常脆弱,IMO)

  • XML serialization
  • Google's Protocol Buffers
  • 谷歌的协议缓冲区

  • Thrift
  • YAML
  • Hand-written serialization/deserialization e.g. using BinaryReader and BinaryWriter
  • 手写的序列化/反序列化,例如使用BinaryReader和BinaryWriter

There are balances in terms of whether the file is human readable, speed, size etc. See this question for answers to a similar question about Java.

在文件是否可读,速度,大小等方面存在余额。请参阅此问题以获取有关Java的类似问题的答案。

#3


You should take a look at the NetDataContractSerializer. You can markup those portions of the struct that you wish to serializer and use a file stream to write them out.

你应该看一下NetDataContractSerializer。您可以标记希望序列化的结构部分,并使用文件流将其写出。

#4


Look at the StructLayoutAttribute

查看StructLayoutAttribute

#5


Use Managed C++ or C++/CLI. It can read your .h file struct. It can read and write using:

使用托管C ++或C ++ / CLI。它可以读取你的.h文件结构。它可以使用以下方式读写:

read(in, &structure, sizeof(structure));
write(out, &structure, sizeof(structure));

and it can transfer that data very simply to anything else in .NET.

它可以非常简单地将数据传输到.NET中的任何其他内容。

#6


You'll have to convert each member of the struct individually using Bitconverter.convert(). This works well when your struct holds numeric data types, but you might have to do something more complex when using structs that contain more complicated data types like strings, arrays, and collections. For purposes like this, you will want to check out the .Net serialization facilities that other have mentioned.

您必须使用Bitconverter.convert()单独转换结构的每个成员。当struct包含数值数据类型时,这很有效,但在使用包含更复杂数据类型(如字符串,数组和集合)的结构时,可能必须执行更复杂的操作。出于这样的目的,您需要查看其他人提到的.Net序列化工具。

#7


You can look into Google Protocol buffers as well. You may not want to add another dependency into your code, but it's meant to allow you to do this sort of thing.

您也可以查看Google协议缓冲区。您可能不希望在代码中添加另一个依赖项,但它意味着允许您执行此类操作。