Hello, I am using StreamReader.ReadLine() to read a text file.

I need a way to move the "pointer" back to the BOF (Beginning of File)
without having to close the object and create a new instance.

Is there any way of doing this? or is there another class within the
framework that will allow me to achieve this?

Any thoughts/ideas would be greatly appreciated

Thanks

JT.

Re: StreamReader how do I move to BOF? by Grzegorz

Grzegorz
Sat Oct 21 15:21:28 CDT 2006

Uzytkownik "Johnnie Walker" <tregoning@gmail.com> napisal w wiadomosci
news:1161454973.941243.41770@k70g2000cwa.googlegroups.com...
> Hello, I am using StreamReader.ReadLine() to read a text file.
>
> I need a way to move the "pointer" back to the BOF (Beginning of File)
> without having to close the object and create a new instance.
>
> Is there any way of doing this? or is there another class within the
> framework that will allow me to achieve this?

If your StreamReader bases on FileStream (or another stream that has working
property Position)you can set Position of the base stream to 0, for example:

class Test
{
public void ReadStreamManyTimes()
{
string path = @"H:\tmp\Test.txt";
using (FileStream fs = new FileStream(path, FileMode.Open))
{
StreamReader reader = new StreamReader(fs);

for (int i = 0; i < 5; i++)
{
ReadAllLines(reader);
reader.BaseStream.Position = 0;
}
}
}

private void ReadAllLines(StreamReader reader)
{
while(!reader.EndOfStream)
{
string oneLine = reader.ReadLine();
System.Console.WriteLine(oneLine);
}
}
}


Best regards,
Grzegorz


Re: StreamReader how do I move to BOF? by Johnnie

Johnnie
Sat Oct 21 16:02:51 CDT 2006

Thanks a lot! very useful!
Grzegorz Danowski wrote:
> Uzytkownik "Johnnie Walker" <tregoning@gmail.com> napisal w wiadomosci
> news:1161454973.941243.41770@k70g2000cwa.googlegroups.com...
> > Hello, I am using StreamReader.ReadLine() to read a text file.
> >
> > I need a way to move the "pointer" back to the BOF (Beginning of File)
> > without having to close the object and create a new instance.
> >
> > Is there any way of doing this? or is there another class within the
> > framework that will allow me to achieve this?
>
> If your StreamReader bases on FileStream (or another stream that has working
> property Position)you can set Position of the base stream to 0, for example:
>
> class Test
> {
> public void ReadStreamManyTimes()
> {
> string path = @"H:\tmp\Test.txt";
> using (FileStream fs = new FileStream(path, FileMode.Open))
> {
> StreamReader reader = new StreamReader(fs);
>
> for (int i = 0; i < 5; i++)
> {
> ReadAllLines(reader);
> reader.BaseStream.Position = 0;
> }
> }
> }
>
> private void ReadAllLines(StreamReader reader)
> {
> while(!reader.EndOfStream)
> {
> string oneLine = reader.ReadLine();
> System.Console.WriteLine(oneLine);
> }
> }
> }
>
>
> Best regards,
> Grzegorz


Re: StreamReader how do I move to BOF? by Peter

Peter
Sat Oct 21 18:20:21 CDT 2006

"Johnnie Walker" <tregoning@gmail.com> wrote in message
news:1161464571.606966.8220@h48g2000cwc.googlegroups.com...
> Thanks a lot! very useful!

I don't know if you saw my reply, but Grzegorz's reply is insufficient. It
does not account for the possibility of buffering within the StreamReader.
You may use the Position property as he suggests, rather than the Seek()
method I mentioned in my post, but in either case you need to call
DiscardBufferedData() to ensure that when you read from the StreamReader
after changing the position, you are actually reading from the new position.

And of course it should go without saying that you should either make sure
that you are always using a seekable stream (if you are always opening a
disk file, this should be the case), or check the CanSeek property of the
stream, or be prepared for a NotSupportedException being thrown. Not all
Streams are seekable.

Pete



Re: StreamReader how do I move to BOF? by Johnnie

Johnnie
Sun Oct 22 12:08:49 CDT 2006

Hey Pete, yes I saw yours and actually used it, thanks a lot.

JT.

Peter Duniho wrote:
> "Johnnie Walker" <tregoning@gmail.com> wrote in message
> news:1161464571.606966.8220@h48g2000cwc.googlegroups.com...
> > Thanks a lot! very useful!
>
> I don't know if you saw my reply, but Grzegorz's reply is insufficient. It
> does not account for the possibility of buffering within the StreamReader.
> You may use the Position property as he suggests, rather than the Seek()
> method I mentioned in my post, but in either case you need to call
> DiscardBufferedData() to ensure that when you read from the StreamReader
> after changing the position, you are actually reading from the new position.
>
> And of course it should go without saying that you should either make sure
> that you are always using a seekable stream (if you are always opening a
> disk file, this should be the case), or check the CanSeek property of the
> stream, or be prepared for a NotSupportedException being thrown. Not all
> Streams are seekable.
>
> Pete