Using structs in C# to read data

前端 未结 2 1256
清歌不尽
清歌不尽 2021-01-26 19:46

Say I have a struct defined as such

struct Student
{
  int age;
  int height;
  char[] name[12];
}

When I\'m reading a binary file, it looks so

2条回答
  •  旧巷少年郎
    2021-01-26 20:13

    There is not, AFAIK, a low-level direct-layout struct reader built into .NET. You would want want to look at BinaryReader, reading each field in turn? Basically, ReadInt32() twice, and ReadChars(). Pay particular attention to the encoding of the character data (ASCII? UTF8? UTF-16?) and the endianness of the integers.

    Personally, I'd look more at using a dedicated cross-platform serializer!

提交回复
热议问题