Reading from a binary file error

本秂侑毒 提交于 2019-12-25 14:23:26

问题


Trying to read simple record structure from Binary file, but get the following error message. What is the problem?

An unhandled exception of type 'System.IO.EndOfStreamException' occurred in mscorlib.dll

Additional information: Unable to read beyond the end of the stream.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Reading_from_Binary_File
{
    class Program
    {
        struct TBook
        {
            public string author;
            public string title;
            public string genre; //TGenreTypes genre;
            public int bookid;
        };

        static void Main(string[] args)
        {
            FileStream currentFile;
            BinaryReader readerFromFile;
            currentFile = new FileStream("Test.bin", FileMode.Open);
            readerFromFile = new BinaryReader(currentFile);

            TBook myBooks;
            do
            {
                //Now read from file and write to console window
                 myBooks.title = readerFromFile.ReadString();
                 myBooks.author = readerFromFile.ReadString();
                 myBooks.genre = readerFromFile.ReadString();
                // myBooks.genre = (TGenreTypes)Enum.Parse(typeof(TGenreTypes),readerFromFile.ReadString());
                myBooks.bookid = readerFromFile.ReadInt16();
                Console.WriteLine("Title: {0}", myBooks.title);
                Console.WriteLine("Author: {0}", myBooks.author);
                Console.WriteLine("Genre: {0}", myBooks.genre);
                Console.WriteLine("BookID: {0}", myBooks.bookid);
            }
            while (currentFile.Position < currentFile.Length);

            //close the streams
            currentFile.Close();
            readerFromFile.Close();

            Console.ReadLine();
        }
    }
}

Update: I also tried

while (currentFile.Position < currentFile.Length);
{
    ...
}

but I get the same error.


回答1:


Try exchanging

myBooks.bookid = readerFromFile.ReadInt16();

with

myBooks.bookid = readerFromFile.ReadInt32();

as by default the int is an alias for System.Int32.

In your structure

struct TBook
{
    public string author;
    public string title;
    public string genre; //TGenreTypes genre;
    public int bookid;
};

you have specified int bookid which would then be a System.Int32. So reading just 2 bytes instead of 4 bytes will result in having 2 bytes left in the stream. So the loop won't break. In the loop you will then try to read another "set" of data which isn't there (just the 2 bytes).




回答2:


Reverse your do...while to a while (do) like so:

while (currentFile.Position < currentFile.Length)            
{
    //Now read from file and write to console window
    . . .
}

In this way, the test is made as to reaching the "danger zone" prior to actually attempting to access that position. If you wait to check until after the attempt, the last attempt (as you found out) will fail.

You might want to czech this out.



来源:https://stackoverflow.com/questions/34616165/reading-from-a-binary-file-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!