BinaryReader-reading返回1条记录,而不是多条记录

本文关键字:记录 返回 1条 BinaryReader-reading | 更新日期: 2023-09-27 18:27:40

我正在使用BinaryWriter将数据写入日志文件。

然而,当我使用BinaryReader检索数据(在循环中)时,我可以读取前3次写入(integer、integer、byte[]),但循环中读取另一次3的下一次迭代似乎只获取了其余的数据(我无法处理它)。

这是代码:

写入代码:

writer.Write(header.StructID);
writer.Write(data.Length);
writer.Write(data);

读取代码:

using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (BinaryReader br = new BinaryReader(fs))
    {
        long bytesRead = 0;
        long readerLen = br.BaseStream.Length;
        //read 1st record
        int id = br.ReadInt32();
        int len = br.ReadInt32();
        byte[] data = br.ReadBytes(len);
        bytesRead += (sizeof(int) * 2) + data.Length;
        while (bytesRead < readerLen)
        {
            //TODO:process data
            //read next
            id = br.ReadInt32();
            len = br.ReadInt32();
            data = br.ReadBytes(len);
            bytesRead += (sizeof(int) * 2) + data.Length;
        }
    }
}

感谢您帮助解决问题!

编辑:

我将二进制写入代码包含在using语句中,并且只允许它执行1次,这样我就可以确切地知道写入了多少字节。当我处理读取的代码时,basestream.length要大得多(即,我写入80个字节,basestream.length显示1144)。

    using (writer = new BinaryWriter(File.Open(filename, FileMode.OpenOrCreate)))
            {
                long pos = writer.BaseStream.Position;
                writer.Write(header.StructID);
                writer.Write(data.Length);
                writer.Write(data);
                m_LoggingEnabled = false;
            }

这是二进制数据:

CE 00 00 00 48 00 00 00 AD A2 3B 94 76 08 A7 3E 7A 9A 80 9D CC 1A 2B 3F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 BF 91 01 00 00 00 00 00 00 00 00 00 00 00 00 F0 3F 01 00 00 00 00 00 00 00 CE 00 00 00 48 00 00 00 AD A2 3B 94 76 08 A7 3E 7A 9A 80 9D CC 1A 2B 3F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 BF 92 01 00 00 00 00 00 00 00 00 00 00 00 2A 7A BE 01 00 00 00 50 64 63 3D CE 00 00 00 48 00 00 00 D8 41 B1 19 01 A3 86 BE E2 E2 7A 22 6F 1F 2B 3F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 BF 91 01 00 00 00 00 00 00 00 00 00 00 00 00 F0 3F 01 00 00 00 00 00 00 00

我现在真的很困惑!

BinaryReader-reading返回1条记录,而不是多条记录

您的BinaryReader代码是正确的,尽管您可以对其进行重构,使其不那么重复,更简洁。

根据你有限的描述,我认为你写错了,而不是读错了。确保header.StructIDdata.Length是整数类型。

由于您是从计时器调用BinaryReader,因此您必须注意Write操作将从现有文件的开头进行写入,因为您尚未将写入流推进到正确的位置。这可能会导致一些不必要的行为(覆盖现有数据等),并且可能会导致您的阅读问题您应该以Append模式打开文件以防止这种情况发生。

以下是基于您的示例使用BinaryReader和BinaryWriter编写和阅读的正确示例。它在文件上使用AppendMode来防止您看到的问题(我在for循环中重新打开文件,以模拟计时器重新打开文件并向其写入内容):

var stuffToWrite = new List<byte[]>()
{
    new byte[72],
        new byte[72],
        new byte[72],
};
for (int i = 0; i < stuffToWrite.Count; i++)
{
    using (var file = File.Open(filename, FileMode.Append))
    using (var binaryWriter = new BinaryWriter(file))
    {
        binaryWriter.Write(206);
        binaryWriter.Write((stuffToWrite[i].Length));
        binaryWriter.Write(stuffToWrite[i]);
    }
}
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var br = new BinaryReader(fs))
{
    do
    {
        int id = br.ReadInt32();
        int len = br.ReadInt32();
        byte[] data = br.ReadBytes(len);
        // Process Data
    } while (br.BaseStream.Position < br.BaseStream.Length);
}