读入包含未知数量结构的二进制文件 (C#)

发布于 2024-09-14 16:56:10 字数 1057 浏览 12 评论 0原文

好的,所以我目前有一个包含未知数量的结构的二进制文件,如下所示:

private struct sTestStruct
{
    public int numberOne;
    public int numberTwo;
    public int[] numbers; // This is ALWAYS 128 ints long.
    public bool trueFalse;
}

到目前为止,我使用以下命令将所有结构读入列表<>:

List<sTestStruct> structList = new List<sTestStruct>();

while (binReader.BaseStream.Position < binReader.BaseStream.Length)
{
    sTestStruct temp = new sTestStruct();
    temp.numberOne = binReader.ReadInt32();
    temp.numberTwo = binReader.ReadInt32();
    temp.numbers = new int[128];
    for (int i = 0; i < temp.numbers.Length; i++)
    {
        temp.numbers[i] = binReader.ReadInt32();
    }
    temp.trueFalse = binReader.ReadBoolean();

    // Add to List<>
    structList.Add(temp);
}

我真的不想这样做,因为只有一个结构的数量可以一次显示给用户,因此一次读取多个记录是没有意义的。所以我认为我可以使用以下内容读取特定记录:

fileStream.Seek(sizeof(sTestStruct) * index, SeekOrigin.Begin);

但它不会让我这样做,因为它不知道 sTestStruct 的大小,该结构不会让我预定义数组大小,那么我该如何处理呢? ?

Ok, so I currently have a binary file containing an unknown number of structs like this:

private struct sTestStruct
{
    public int numberOne;
    public int numberTwo;
    public int[] numbers; // This is ALWAYS 128 ints long.
    public bool trueFalse;
}

So far, I use the following to read all the structs into a List<>:

List<sTestStruct> structList = new List<sTestStruct>();

while (binReader.BaseStream.Position < binReader.BaseStream.Length)
{
    sTestStruct temp = new sTestStruct();
    temp.numberOne = binReader.ReadInt32();
    temp.numberTwo = binReader.ReadInt32();
    temp.numbers = new int[128];
    for (int i = 0; i < temp.numbers.Length; i++)
    {
        temp.numbers[i] = binReader.ReadInt32();
    }
    temp.trueFalse = binReader.ReadBoolean();

    // Add to List<>
    structList.Add(temp);
}

I don't really want to do this, as only one of the structs can be displayed to the user at once, so there is no point reading in more than one record at a time. So I thought that I could read in a specific record using something like:

fileStream.Seek(sizeof(sTestStruct) * index, SeekOrigin.Begin);

But it wont let me as it doesn't know the size of the sTestStruct, the structure wont let me predefine the array size, so how do I go about this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-09-21 16:56:11

从我读到的所有内容来看,您这样做的方式是读取二进制数据的最佳方法,因为它出现问题的陷阱最少。

From everything I have read, the way you are doing it is the best method to read in binary data as it has the fewest gotchas where things can go wrong.

海未深 2024-09-21 16:56:11

像这样定义您的结构:

struct sTestStruct
{
    public int numberOne;
    public int numberTwo;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
    public int[] numbers; // This is ALWAYS 128 ints long. 
    public bool trueFalse;
}

并使用 Marshal.Sizeof(typeof(sT​​estStruct))

Define your struct like this:

struct sTestStruct
{
    public int numberOne;
    public int numberTwo;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
    public int[] numbers; // This is ALWAYS 128 ints long. 
    public bool trueFalse;
}

And use Marshal.Sizeof(typeof(sTestStruct)).

淡墨 2024-09-21 16:56:10

sTestStruct 并不存储在连续的内存区域中,并且 sizeof(sT​​estStruct) 与文件中记录的大小没有直接关系。 numbers 成员是对您在阅读代码中分配的数组的引用。

但是您可以轻松地在代码中指定记录大小,因为它是一个常量值。此代码将查找 index 处的记录。然后,您可以使用循环体读取一条记录。

const Int32 RecordSize = (2 + 128)*sizeof(Int32) + sizeof(Boolean);
fileStream.Seek(RecordSize * index, SeekOrigin.Begin); 

如果您有许多不同的固定大小的记录,并且您担心手动输入每个记录的记录大小容易出错,您可以设计一个基于反射和自定义属性的方案。

创建一个属性来定义数组的大小:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
sealed class ArraySizeAttribute : Attribute {

  public ArraySizeAttribute(Int32 length) {
    Length = length;
  }

  public Int32 Length { get; private set; }

}

在您的记录类型上使用该属性:

private struct sTestStruct {   
  public int numberOne;   
  public int numberTwo;   
  [ArraySize(128)]
  public int[] numbers; // This is ALWAYS 128 ints long.   
  public bool trueFalse;   
}

然后您可以使用以下示例代码计算记录的大小:

Int32 GetRecordSize(Type recordType) {
  return recordType.GetFields().Select(fieldInfo => GetFieldSize(fieldInfo)).Sum();
}

Int32 GetFieldSize(FieldInfo fieldInfo) {
  if (fieldInfo.FieldType.IsArray) {
    // The size of an array is the size of the array elements multiplied by the
    // length of the array.
    var arraySizeAttribute = (ArraySizeAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof(ArraySizeAttribute));
    if (arraySizeAttribute == null)
      throw new InvalidOperationException("Missing ArraySizeAttribute on array.");
    return GetTypeSize(fieldInfo.FieldType.GetElementType())*arraySizeAttribute.Length;
  }
  else
    return GetTypeSize(fieldInfo.FieldType);
}

Int32 GetTypeSize(Type type) {
  if (type == typeof(Int32))
    return 4;
  else if (type == typeof(Boolean))
    return 1;
  else
    throw new InvalidOperationException("Unexpected type.");
}

像这样使用它:

var recordSize = GetRecordSize(typeof(sTestStruct));
fileStream.Seek(recordSize * index, SeekOrigin.Begin); 

您可能需要对此代码进行一些扩展才能使用它在生产中。

The sTestStruct is not stored in one consecutive are of memory and sizeof(sTestStruct) is not directly related to the size of the records in the file. The numbers members is a reference to an array which you allocate youself in your reading code.

But you can easily specify the record size in code since it is a constant value. This code will seek to the record at index. You can then read one record using the body of your loop.

const Int32 RecordSize = (2 + 128)*sizeof(Int32) + sizeof(Boolean);
fileStream.Seek(RecordSize * index, SeekOrigin.Begin); 

If you have many different fixed sized records and you are afraid that manually entering the record size for each record is error prone you could devise a scheme based on reflection and custom attributes.

Create an attribute to define the size of arrays:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
sealed class ArraySizeAttribute : Attribute {

  public ArraySizeAttribute(Int32 length) {
    Length = length;
  }

  public Int32 Length { get; private set; }

}

Use the attribute on your record type:

private struct sTestStruct {   
  public int numberOne;   
  public int numberTwo;   
  [ArraySize(128)]
  public int[] numbers; // This is ALWAYS 128 ints long.   
  public bool trueFalse;   
}

You can then compute the size of the record using this sample code:

Int32 GetRecordSize(Type recordType) {
  return recordType.GetFields().Select(fieldInfo => GetFieldSize(fieldInfo)).Sum();
}

Int32 GetFieldSize(FieldInfo fieldInfo) {
  if (fieldInfo.FieldType.IsArray) {
    // The size of an array is the size of the array elements multiplied by the
    // length of the array.
    var arraySizeAttribute = (ArraySizeAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof(ArraySizeAttribute));
    if (arraySizeAttribute == null)
      throw new InvalidOperationException("Missing ArraySizeAttribute on array.");
    return GetTypeSize(fieldInfo.FieldType.GetElementType())*arraySizeAttribute.Length;
  }
  else
    return GetTypeSize(fieldInfo.FieldType);
}

Int32 GetTypeSize(Type type) {
  if (type == typeof(Int32))
    return 4;
  else if (type == typeof(Boolean))
    return 1;
  else
    throw new InvalidOperationException("Unexpected type.");
}

Use it like this:

var recordSize = GetRecordSize(typeof(sTestStruct));
fileStream.Seek(recordSize * index, SeekOrigin.Begin); 

You will probably have to expand a little on this code to use it in production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文