简体   繁体   English

将具有多种数据类型的二进制文件解析为数组

[英]Parsing a binary file with multiple data types into an array

I've created multiple binary files, each containing 2 strings, 2 chars, 1 double, and 1 int. 我创建了多个二进制文件,每个文件包含2个字符串,2个字符,1个double和1个int。 The data, when read, is 读取时的数据为

Fokker DR 1 
Germany 
D 
A 
1000.0 
13

And the binary file reads as follows: 二进制文件的内容如下: dat文件文字

I am trying to parse the dat file into an array so I can label each data type with the appropriate name such as 我正在尝试将dat文件解析为数组,以便可以使用适当的名称(例如,

Name: Fokker DR 1
Country: Germany
Attack Mode: D
Turn Mode: A
etc.

But I am having trouble because, when trying to use StreamReader and split the binary file at every instance of a whitespace, I run into problems due to the fact that the Name (Fokker DR 1) contains multiple spaces that should not be split. 但是我遇到了麻烦,因为当尝试使用StreamReader并在空白的每个实例处分割二进制文件时,由于名称(Fokker DR 1)包含不应分割的多个空格的事实,我遇到了问题。

Here is my code for my attempted split: 这是我尝试分割的代码:

if (file.Contains("plane1"))
{
    StreamReader reader = new StreamReader("plane1.dat");
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        string[] data = line.Split(' ');
        bin.ReadBinary("plane1.dat");
        Console.WriteLine("Name: " + data[0]);
        Console.WriteLine("Country: " + data[1]);
        Console.WriteLine("Turn Mode: " + data[2]);
        Console.WriteLine("Attack Mode: " + data[3]);
        Console.WriteLine("Cost: " + data[4]);
        Console.WriteLine("Max Damage: " + data[5]);
    }
}

Is there any sort of way to split at every instance of a different data type or is there another way to go about labeling the data in my binary file?? 有没有什么方法可以在不同数据类型的每个实例处进行拆分,或者还有另一种方法来标记我的二进制文件中的数据?

The BinaryFormatter will not handle that file format. BinaryFormatter将不处理该文件格式。 Given the small size and straight forward specs some ustom read logic should do the trick. 鉴于其体积小巧和简单明了的规格,一些超自然的读取逻辑应该可以解决问题。

If you open your file with a BinaryReader you can decide for your self what to do with the next byte or bytes you're about to read. 如果使用BinaryReader打开文件,则可以自行决定如何处理下一个或多个要读取的字节。 If the format of the file is not to complex this is easy doable. 如果文件格式不复杂,则很容易做到。 Based on your specs I created this code to read your file: 根据您的规格,我创建了以下代码来读取您的文件:

using(var br = new BinaryReader(
    File.Open(@"c:\tmp\plane.dat", FileMode.Open),
    Encoding.ASCII))
{
    while(br.BaseStream.Position < br.BaseStream.Length)
    {
        var name = br.ReadString();
        var country = br.ReadString();
        var turnmode = br.ReadChar();
        var attackmode = br.ReadChar();
        var cost = br.ReadDouble();
        var maxdamage = br.ReadInt32();

        // you can use above vars what ever you need to do 
        // with them, writing to the console or adding to 
        // a list for example
        // Planes.Add(new Plane {Name = name});
    }
}

In my test a file I created matched your binary format. 在测试中,我创建的文件与您的二进制格式匹配。 As you didn't show where the next record start you might find that you need to do an extra read, for example with a ReadByte but that depends on your actual structure. 由于您没有显示下一条记录的开始位置,因此您可能会发现需要进行额外的读取,例如使用ReadByte但这取决于您的实际结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM