C#无法在流的结尾之外进行读取

SeasonRun 2012-09-21 10:14:31
public void Reading()
{
if (File.Exists("C:\\IPChange.txt"))
{
FileStream fs =new FileStream("C:\\IPChange.txt", FileMode.Open,FileAccess.Read,FileShare.Read);
BinaryReader reader = new BinaryReader(fs);
text11.Text = reader.ReadString();
text12.Text = reader.ReadString();
text13.Text = reader.ReadString();
text14.Text = reader.ReadString();
text15.Text = reader.ReadString();
text21.Text = reader.ReadString();
text22.Text= reader.ReadString();
text23.Text = reader.ReadString();
text24.Text = reader.ReadString();
text25.Text = reader.ReadString();
reader.Close();
fs.Close();
}
}运行提醒无法在流的结尾之外进行读取,什么意思啊,要怎么解决,请大家帮忙解决,谢谢!
...全文
4063 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2012-09-21
  • 打赏
  • 举报
回复
你的位置已经超出了流的范围,比如流就有10个字节,你非得去读第20个字节
xdq025 2012-09-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haiziccc 的回复:]

引用 3 楼 的回复:
C# code


while(!FileStream.EOF)
{
//.......
}
FileStream.EOF,EOF是什么?
[/Quote]
搞错对象了 - -
不过这个BinaryReader.ReadString()的方法不能正常使用,我测试的时候,第一次读取就异常了,此时_Position=0,_Length=23建议使用StreamReader
[code=C#]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Text;

namespace test
{
public partial class test :TestPageBase
{
public class MyReader : BinaryReader
{
public MyReader(Stream stream):base(stream)
{

}
public MyReader(Stream stream, Encoding code)
: base(stream, code)
{
}
private long _Length = -1;
private long _Position = -1;
public override string ReadString()
{
if (_Length < 0)
{ _Length = BaseStream.Length; }
_Position = BaseStream.Position;
if (_Position < _Length)
return base.ReadString();
else
return "";
}
}
protected void Page_Load(object sender, EventArgs e)
{
//FileStream file = File.OpenRead(@"D:\Test");
//if (file.CanRead)
//{
// MyReader br = new MyReader(file, Encoding.UTF8);
// string str = "InitData";
// while (!string.IsNullOrEmpty(str))
// {
// str = br.ReadString();
// Say(str);
// }
//}
StreamReader sr = new StreamReader(@"D:\Test");
while (!sr.EndOfStream)
{
Say(sr.ReadLine());
}
}

}
}
[/Code]
SeasonRun 2012-09-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
C# code


while(!FileStream.EOF)
{
//.......
}
[/Quote]FileStream.EOF,EOF是什么?
xdq025 2012-09-21
  • 打赏
  • 举报
回复
[code=C#]
while(!FileStream.EOF)
{
//.......
}
[/Code]
SeasonRun 2012-09-21
  • 打赏
  • 举报
回复
那要怎么解决呢?

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧