2012-08-16 72 views
6

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: 
+0

这个答案已经帮助我,当我得到了同样的错误: https://stackoverflow.com/questions/23865865/reading-xls-stream-with-getresponsestream-and-spreadsheetdocument-open – gabics 2017-09-17 02:21:28

回答

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个字节,如果它们的存在。

+0

Alexei Levenkov我试过你的代码,并以结果结束:-1325399654那就是我在textBox1中看到的。可能来自200MB文件? – 2012-08-16 02:44:24

+0

@DanielLip,-1325399654有什么问题?你能指望什么?这是很难争论,如果它有任何更好或比42或13差。 – 2012-08-16 02:46:45

+0

阿列克谢我需要从这个巨大的200MB文件upack文件。这就是为什么我认为200MB的文件大小会给更多的数字。但我想你是对的,我只是不知道下一步该怎么做,我得到了这个数字。 – 2012-08-16 02:50:01

4

您应该使用更可靠找出你在什么时候结束时的方式,而不是用sizeof(int)滚动你自己的计数器。你的方法可能不够精确,而且你使用不安全的代码的事实也不太好。

一种方式探头,如果你是在流的结束与否是使用PeekChar方法:

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

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

+0

dasblinkenlight即时得到相同错误。我刚刚使用示例解决方案更新了我的问题,但我得到了相同的erorr异常。 – 2012-08-16 02:40:56

+0

@DanielLip我怀疑这个方法不够可靠。尝试使用更新中的代码,看它是否更好,但如果二进制文件中的字节数不能被磁盘上的'Int32'大小整除,将会失败。 – dasblinkenlight 2012-08-16 02:51:20

相关问题