Home All Groups Group Topic Archive Search About

StreamReader how do I move to BOF?

Author
21 Oct 2006 6:22 PM
Johnnie Walker
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.

Author
21 Oct 2006 8:21 PM
Grzegorz Danowski
Uzytkownik "Johnnie Walker" <tregon***@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
Author
21 Oct 2006 9:02 PM
Johnnie Walker
Thanks a lot! very useful!
Grzegorz Danowski wrote:
Show quote
> Uzytkownik "Johnnie Walker" <tregon***@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
Author
21 Oct 2006 11:20 PM
Peter Duniho
"Johnnie Walker" <tregon***@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
Author
22 Oct 2006 5:08 PM
Johnnie Walker
Hey Pete, yes I saw yours and actually used it, thanks a lot.

JT.

Peter Duniho wrote:
Show quote
> "Johnnie Walker" <tregon***@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

AddThis Social Bookmark Button