2012-08-16 30 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

相關問題