We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

reset a streamreader to the beginning?

riceman0
riceman0 asked
on
Medium Priority
4,602 Views
Last Modified: 2010-05-19

Hey, I am using a System.IO.StreamReader, specifically the ReadLine method to parse through an ascii file and extract the data I need.  I am using this because I used to use the old line input command a lot (actually have used it for about 15 years so this a weird shift).  Anyway, the ReadLine method, called successively, reads successive lines of the file.  Is there a way to reset this to the beginning, so that the next ReadLine method reads the first line, without closing and reinstantiating the reader?

Thanks.
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Yes...like this:

        Dim sr As New System.IO.StreamReader("c:\somefile.txt")
        Dim line As String

        Debug.WriteLine("Contents:")
        line = sr.ReadLine
        While Not IsNothing(line)
            Debug.WriteLine(line)
            line = sr.ReadLine
        End While

        sr.DiscardBufferedData()
        sr.BaseStream.Seek(0, IO.SeekOrigin.Begin)

        Debug.WriteLine("Contents Again:")
        line = sr.ReadLine
        While Not IsNothing(line)
            Debug.WriteLine(line)
            line = sr.ReadLine
        End While

        sr.Close()

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:

Thanks.  Question: I guess that means that the StreamReader contains a BaseStream object, rather than derives from the BaseStream class (in which case the basestream methods would just appear as streamreader methods, right?).  Any idea why they did it that way?  (As you might remember I'm just getting the hang of .NET framework...)
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
That's a good question...

I think they did it that way so they could control how the underlying stream is accessed via a StreamReader.  This makes it ~easier~ to use for the end user because it is harder to screw up the way the file is read since you only have a limited way in which you can access the file stream.

They provided other classes to access file streams in different ways or at lower levels.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.