我得到错误无法读取超出流的结尾为什么?

时间:2012-08-16 02:24:32

标签: c#

testing.res文件大小为240MB。 我想读它。但我在这一行得到错误:

int v = br.ReadInt32();

EndOfStreamException 无法阅读超出流的结尾

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            R();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void R()
        {
            using (var br = new System.IO.BinaryReader(File.Open(@"d:\testing.res", FileMode.Open)))
            {
                // 2.
                // Position and length variables.
                int pos = 0;
                // 2A.
                // Use BaseStream.
                int length = (int)br.BaseStream.Length;
                while (br.BaseStream.Position < length)
                {
                    // 3.
                    // Read integer.
                    int v = br.ReadInt32();
                    textBox1.Text = v.ToString();
                }

            }
        }
    }
}

例外:

System.IO.EndOfStreamException was unhandled
  Message=Unable to read beyond the end of the stream.
  Source=mscorlib
  StackTrace:
       at System.IO.BinaryReader.FillBuffer(Int32 numBytes)
       at System.IO.BinaryReader.ReadInt32()
       at WindowsFormsApplication1.Form1.R() in D:\C-Sharp\BinaryReader\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 41
       at WindowsFormsApplication1.Form1..ctor() in D:\C-Sharp\BinaryReader\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 19
       at WindowsFormsApplication1.Program.Main() in D:\C-Sharp\BinaryReader\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

3 个答案:

答案 0 :(得分:4)

您应该使用更可靠的方法来确定何时在流的末尾,而不是使用sizeof(int)滚动自己的计数器。你的方法可能不够精确,而你使用不安全代码的事实也不太好。

如果您位于流的末尾,则单向探测是使用PeekChar方法:

while (br.PeekChar() != -1)
{
    // 3.
    // Read integer.
    int v = br.ReadInt32();
    textBox1.Text = v.ToString();
}

更常见的解决方案是在实际的整数列表前面写入要保存在二进制文件中的int个数。这样您就可以知道何时停止而不依赖于流的长度或位置。

答案 1 :(得分:3)

代码失败的一个原因是文件是否包含额外字节(即7字节长文件)。您的代码将在最后3个字节中跳转。

要修复 - 请考虑提前计算整数数并使用for来读取:

var count = br.BaseStream.Length / sizeof(int);
for (var i = 0; i < count; i++)
{
  int v = br.ReadInt32();
  textBox1.Text = v.ToString();
}

请注意,如果存在,则此代码将忽略最后1-3个字节。

答案 2 :(得分:0)

二进制读取器未指向流的开头。

在读取整数值之前使用此代码:

br.BaseStream.Seek(0, SeekOrigin.Begin);