将二进制文件数据读入结构列表

时间:2018-12-13 16:54:22

标签: c# struct binaryfiles

我有一个简单的过程,可以将图书馆图书清单(类型为TBook)写入二进制文件,如下所示:

static void SaveToFile(List<TBook> lib)
    {
        FileStream currentFile;
        BinaryWriter writerToFile;
        currentFile = new FileStream("MyLibrary.bin", FileMode.Create);
        writerToFile = new BinaryWriter(currentFile);
        foreach (TBook book in lib)
        {
            writerToFile.Write(book.Title);
            writerToFile.Write(book.Author);
            writerToFile.Write(book.Genre);
            writerToFile.Write(book.BookID);
        }
        writerToFile.Close();
        currentFile.Close();
    }

但是,当尝试读取二进制文件并将内容加载到列表中时,出现错误:

  

mscorlib.dll中发生了'System.IO.EndOfStreamException'类型的未处理异常

     

其他信息:无法读到流的末尾。

这是我的子例程,试图再次将二进制文件读回结构:

static List<TBook> LoadDataFromFile (List<TBook>library)
    {
        FileStream currentFile;
        BinaryReader readerFromFile;
        currentFile = new FileStream("MyLibrary.bin", FileMode.Open);
        readerFromFile= new BinaryReader(currentFile);

        while (currentFile.Position < currentFile.Length)
        {
            TBook CurrentRecord = new TBook();
            CurrentRecord.Title = readerFromFile.ReadString();
            CurrentRecord.Author = readerFromFile.ReadString();
            CurrentRecord.Genre = readerFromFile.ReadString();
            CurrentRecord.BookID = readerFromFile.ReadInt16();
            library.Add(CurrentRecord);                
       }

        readerFromFile.Close();
        currentFile.Close();
        return library;
    }

我认为问题出在那行:

while (currentFile.Position < currentFile.Length) 

注意:Struct的设置如下:

struct TBook
    {
        public string Title;
        public string Author;
        public string Genre;
        public int BookID;
    }

1 个答案:

答案 0 :(得分:1)

将数据序列化为二进制时,反序列化代码必须严格遵循序列化代码;否则,反序列化器会开始从相邻位置读取垃圾,最终导致异常或用错误的数据静默填充结构。

这对电话不匹配:

writerToFile.Write(book.BookID);
....
CurrentRecord.BookID = readerFromFile.ReadInt16();

很难看到这个问题,因为BinaryWriter重载了Write方法。由于book.BookID的类型为Int32的别名int,因此对Write的调用被解析为Write(Int32)。因此,相应的读取还必须读取Int32,而不是Int16

CurrentRecord.BookID = readerFromFile.ReadInt32();