即时通讯出现错误为什么无法读取流末尾的内容? [英] Im getting error Unable to read beyond the end of the stream why?
本文介绍了即时通讯出现错误为什么无法读取流末尾的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
testing.res文件的大小为240MB.我想读.但是我在这条线上得到了错误:
The testing.res file is 240MB size. I want to read it. But im getting the error on this line:
int v = br.ReadInt32();
EndOfStreamException无法读到流的末尾
EndOfStreamException Unable to read beyond the end of the stream
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:
推荐答案
代码可能失败的原因之一是文件中是否包含额外的字节(即7字节长的文件).您的代码将在最后3个字节跳闸.
One reason your code could fail is if file contains extra bytes (i.e. 7 byte long file). Your code will trip on last 3 bytes.
要修复-考虑预先计算整数数,并使用 for
进行读取:
To fix - consider computing number of integers in advance and using for
to read:
var count = br.BaseStream.Length / sizeof(int);
for (var i = 0; i < count; i++)
{
int v = br.ReadInt32();
textBox1.Text = v.ToString();
}
请注意,该代码将忽略后1-3个字节(如果有的话).
Note that this code will simply ignore last 1-3 bytes if they are there.
这篇关于即时通讯出现错误为什么无法读取流末尾的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文