2012-08-16 16 views
6

परीक्षण.res फ़ाइल 240 एमबी आकार है। मैं इसे पढ़ना चाहता हूं। लेकिन im इस लाइन पर त्रुटि मिल रही है:मुझे त्रुटि मिल रही है स्ट्रीम के अंत से परे पढ़ने में असमर्थ क्यों?

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

उत्तर

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

एलेक्सी लेवेनकोव मैंने आपके कोड की कोशिश की और परिणाम के साथ समाप्त हुआ: -1325399654 जो मैंने टेक्स्टबॉक्स 1 में देखा था। 200 एमबी फ़ाइल से हो सकता है? –

+0

@DanielLip, -1325399654 के साथ क्या गलत है? आप क्या उम्मीद करते हैं? यह तर्क देना मुश्किल है कि यह 42 या 13 से बेहतर या खराब है। –

+0

एलेक्सी मुझे इस विशाल 200 एमबी फ़ाइल से फ़ाइलों को अपनाने की आवश्यकता है। Thats क्यों मैंने सोचा कि 200 एमबी फ़ाइल आकार अधिक संख्या दे देंगे। लेकिन मुझे लगता है कि आप सही हैं, मुझे नहीं पता कि मुझे इस नंबर के साथ क्या करना है। –

4

आप एक और अधिक विश्वसनीय का उपयोग करना चाहिए sizeof(int) के साथ अपना खुद का काउंटर रोल करने के बजाए, स्ट्रीम के अंत में होने पर लगने का तरीका। आपकी विधि पर्याप्त सटीक नहीं हो सकती है, और तथ्य यह है कि आप इसके लिए असुरक्षित कोड का उपयोग कर रहे हैं, यह भी बहुत अच्छा नहीं है।

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

एक अधिक सामान्य समाधान int रों की संख्या है कि आप एक में बचत कर रहे हैं लिखने के लिए:

एक तरह से जांच करता है, तो आप धारा के अंत में नहीं हैं या PeekChar विधि का उपयोग करने के लिए है पूर्णांक की वास्तविक सूची के सामने बाइनरी फ़ाइल। इस तरह आप जानते हैं कि लंबाई या धारा की स्थिति पर भरोसा किए बिना कब रुकना है।

+0

dasblinkenlight im एक ही हो रही है त्रुटि। मैंने अपने नमूना समाधान का उपयोग करके कोड के साथ अपना प्रश्न अपडेट किया है, लेकिन मुझे एक ही एरर अपवाद मिल रहा है। –

+0

@DanielLip मुझे संदेह था कि विधि पर्याप्त विश्वसनीय नहीं होगी। यह देखने के लिए अद्यतन से कोड आज़माएं कि यह बेहतर है या नहीं, हालांकि यह विफल होगा यदि आपकी बाइनरी फ़ाइल में बाइट्स की संख्या डिस्क पर 'Int32' के आकार से विभाजित नहीं है। – dasblinkenlight

संबंधित मुद्दे