如何在StreamReader类中使用seek()? [英] how to use seek() in StreamReader class?

查看:217
本文介绍了如何在StreamReader类中使用seek()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在FileStream类中使用seek(),我们知道。

但我发现在StreamReader中seek()无法正常工作。

谁可以告诉我如何在StreamReader中正确使用seek()?

非常感谢!

我使用seek()这样:

StreamReader r;

.......

r.BaseStream.seek(.....);

We can use seek() in the FileStream class,as we know.
But I found that seek() is not work correctly in StreamReader.
Who can tell me how to use seek() correctly in StreamReader?
thanks a lot!
I use the seek() like this:
StreamReader r;
.......
r.BaseStream.seek(.....);

推荐答案

Tiger,


尝试调用r.DiscardBufferedData()来清除流的数据缓冲区。

如果您仍然遇到问题,请发布您的代码以及您想要完成的内容。


希望这会有所帮助。
Tiger,

Try calling r.DiscardBufferedData() to clear the stream''s buffer of data.
If you continue to have problems please post your code and exactly what you
want to accomplish.

Hope this helps.


Brian Brown,

我的代码是这样的:

FileStream fs;

fs = new FileStream (" cc.txt",FileMode.Open,FileAccess.Read);

StreamReader r = new StreamReader(fs);

Console.WriteLine((char)r .Pek());

//r.DiscardBufferedData();

r.BaseStream.Seek(5,SeekOrigin.Current);

//r.DiscardBufferedData();

Console.WriteLine((char)r.Peek ());

cc.txt的内容是:abcdefghijklmnop

我认为在sreen上打印的结果是:

a

f

但实际上是

a

a

为什么?

我试过r.DiscardBufferedData(),这在我的代码中有说明,但

结果是

a



我希望你的回复,非常感谢!

" Brian Brown"写道:
Brian Brown,
My code is like this:
FileStream fs;
fs=new FileStream("cc.txt",FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Console.WriteLine((char)r.Peek());
//r.DiscardBufferedData();
r.BaseStream.Seek(5,SeekOrigin.Current);
//r.DiscardBufferedData();
Console.WriteLine((char)r.Peek());
The content of cc.txt is :abcdefghijklmnop
I thought the result which is printed on sreen is:
a
f
But in fact is
a
a
why?
I have tried r.DiscardBufferedData(),which is remarked in my code,but the
result is
a
?
I hope your reply,thanks a lot!
"Brian Brown" wrote:
Tiger,

尝试调用r.DiscardBufferedData()来清除流的数据缓冲区。
如果继续有问题请发布您的代码以及您想要完成的内容。

希望这会有所帮助。
Tiger,

Try calling r.DiscardBufferedData() to clear the stream''s buffer of data.
If you continue to have problems please post your code and exactly what you
want to accomplish.

Hope this helps.



Tiger ,


我很高兴你发布了你的代码。从早期发布的帖子中我认为你在缓冲区中遗留了数据。

。现在我认为我看到了你想要完成的事情。我将尝试解释?|


Seek是BaseStream的一种方法(即FileStream)。如果你在BaseStream上调用Seek(5,

SeekOrigin.Current)(记住这是FileStream不是/ $
StreamReadera?|)你已经将文件指针向前移动了5地方。如果你调用BaseStream.Read方法,你会看到返回的值是

偏离指针所指向的5个位置。


StreamReader没有Seek这样的方法。既然如此,那么当您调用StreamReader.Peek()时,您将无法获得预期的结果。 Peek()

方法使用不同的数据指针。根据StreamReader指向的位置,它将在流中看到下一个字符

,而不是

BaseStream指针所在的位置。我已经在下面发布了一些示例代码来

证明我的观点。


我希望这有帮助


- -------------

使用System;

使用System.IO;

使用System.Text;


..

..

..

const String FILENAME = @" D :\ cc.txt" ;;


FileStream fs;

fs = new FileStream(FILENAME,FileMode.Open,FileAccess.Read);

StreamReader r = new StreamReader(fs);

Byte [] myByteArray = new Byte [1];

ASCIIEncoding myEncoding = new ASCIIEncoding();


//在基本流上调用搜索(即文件流)

r.BaseStream.Seek(0,SeekOrigin.Begin);

//从文件流中读取第一个字符

r.BaseStream.Read(myByteArray,0,1);

//输出到控制台

Console.WriteLine(myEncoding.GetString(myByteArray));


//在基本流上调用搜索并将其向上移动5个位置
r.BaseStream.Seek(5,SeekOrigin.Current);

//从文件流中读取第一个字符

r.BaseStream。阅读(myByteArray,0,1);

//输出到控制台

Console.WriteLine(myEncoding.GetString(myByteArray));


//关闭溪流

r.Close();

fs.Close();

Tiger,

I am glad that you posted your code. From the earlier posting I was
thinking that you had data left over in the buffer. Now I think that I see
what you are trying to accomplish. I will attempt to explaina?|

Seek is a method of the BaseStream (i.e. FileStream). If you call Seek(5,
SeekOrigin.Current) on the BaseStream (remember this is the FileStream not
the StreamReadera?|) you have moved the file pointer forward 5 places. If you
call the BaseStream.Read method you will see that the returned value is
offset 5 places from where the pointer used to be pointing.

The StreamReader has no such method as Seek. Since this is the case you will
not get the results you expect when you call StreamReader.Peek(). The Peek()
method uses a different pointer to the data. It will see the next character
in the stream according to where the StreamReader is pointing not where the
BaseStream pointer is located. I have posted some sample code below to
demonstrate my point.

I hope this helps

---------------
using System;
using System.IO;
using System.Text;

..
..
..
const String FILENAME = @"D:\cc.txt";

FileStream fs;
fs=new FileStream(FILENAME,FileMode.Open,FileAccess.Read) ;
StreamReader r=new StreamReader(fs);
Byte[] myByteArray = new Byte[1];
ASCIIEncoding myEncoding = new ASCIIEncoding();

//call seek on the base stream (i.e. the filestream)
r.BaseStream.Seek(0,SeekOrigin.Begin);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//call seek on the base stream and move it up 5 places
r.BaseStream.Seek(5,SeekOrigin.Current);
//read the first character from the file stream
r.BaseStream.Read(myByteArray,0,1);
//output to the console
Console.WriteLine(myEncoding.GetString(myByteArray ));

//close the streams
r.Close();
fs.Close();


这篇关于如何在StreamReader类中使用seek()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆