7

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: 
4

3 回答 3

4

您应该使用更可靠的方法来确定您何时处于流的末尾,而不是使用sizeof(int). 您的方法可能不够精确,而且您为此使用了不安全的代码这一事实也不太好。

探测您是否在流的末尾的一种方法是使用该PeekChar方法:

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

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

于 2012-08-16T02:29:58.333 回答
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 个字节,此代码将简单地忽略它们。

于 2012-08-16T02:40:31.457 回答
1

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

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

br.BaseStream.Seek(0, SeekOrigin.Begin);
于 2019-10-22T09:47:03.150 回答