StreamReader.Seek(0, Begin)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joan Reddy

    StreamReader.Seek(0, Begin)

    Can anyone tell me why this code doesn't work for setting the pointer to the
    begining of a file stream?
    This is driving me crazy.

    At the end of Main1, sString2 is the second line of the file, as if the Seek
    never worked.
    Shouldn't sString1 and sString2 each contain the first line of the file?

    To fix this (as in Main2), I need to create a new reader. Is this
    documented behavior, or is this a bug??

    Imports System
    Imports System.IO
    Imports Microsoft.Visua lBasic
    Sub Main1()
    Dim oFileStream As FileStream
    oFileStream = New FileStream("c:m yfile.txt",
    FileMode.Open,F ileAccess.Read)
    Dim oReader As StreamReader
    oReader = New StreamReader(oF ileStream)
    Dim sString1 As String
    sString 1 = oReader.ReadLin e()
    oFileStream .Seek(0, SeekOrigin.Begi n)
    Dim sString2 = oReader.ReadLin e()
    MsgBox(sString1 , sString2)
    End Sub

    Imports System
    Imports System.IO
    Imports Microsoft.Visua lBasic
    Sub Main1()
    Dim oFileStream As FileStream
    oFileStream = New FileStream("c:m yfile.txt",
    FileMode.Open,F ileAccess.Read)
    Dim oReader As StreamReader
    oReader = New StreamReader(oF ileStream)
    Dim sString1 As String
    sString 1 = oReader.ReadLin e()
    oFileStream .Seek(0, SeekOrigin.Begi n)
    oReader = New StreamReader(oF ileStream)
    Dim sString2 = oReader.ReadLin e()
    MsgBox(sString1 , sString2)
    End Sub


  • David Browne

    #2
    Re: StreamReader.Se ek(0, Begin)


    "Joan Reddy" <tjreddy@earthl ink.net> wrote in message
    news:Dndhe.333$ w21.173@newsrea d3.news.atl.ear thlink.net...[color=blue]
    > Can anyone tell me why this code doesn't work for setting the pointer to
    > the begining of a file stream?
    > This is driving me crazy.
    >
    > At the end of Main1, sString2 is the second line of the file, as if the
    > Seek never worked.
    > Shouldn't sString1 and sString2 each contain the first line of the file?
    >
    > To fix this (as in Main2), I need to create a new reader. Is this
    > documented behavior, or is this a bug??[/color]

    Does it have to be one or the other? :)

    StreamReader has an internal buffer of chars which it fills during the first
    call to ReadLine(). That buffer is probably large enough to hold the first
    two lines. So when you issue the second ReadLine() you get the second line.
    At some point StreamReader will read more bytes from the underlying stream
    and decode them into its char buffer. Only then will it re-read the first
    line.

    Lots of the System.IO reader writer classes employ buffering, making
    manipulating the underlying streams tricky.

    David


    Comment

    • Joan Reddy

      #3
      Re: StreamReader.Se ek(0, Begin)

      I have to create a new stream reader every time I move backwards in the
      file. This seems strange to me, but if it is the only way it works, then
      I'll do it.

      "David Browne" <davidbaxterbro wne no potted meat@hotmail.co m> wrote in
      message news:%23JYaBCDW FHA.3540@TK2MSF TNGP15.phx.gbl. ..[color=blue]
      >
      > "Joan Reddy" <tjreddy@earthl ink.net> wrote in message
      > news:Dndhe.333$ w21.173@newsrea d3.news.atl.ear thlink.net...[color=green]
      >> Can anyone tell me why this code doesn't work for setting the pointer to
      >> the begining of a file stream?
      >> This is driving me crazy.
      >>
      >> At the end of Main1, sString2 is the second line of the file, as if the
      >> Seek never worked.
      >> Shouldn't sString1 and sString2 each contain the first line of the file?
      >>
      >> To fix this (as in Main2), I need to create a new reader. Is this
      >> documented behavior, or is this a bug??[/color]
      >
      > Does it have to be one or the other? :)
      >
      > StreamReader has an internal buffer of chars which it fills during the
      > first call to ReadLine(). That buffer is probably large enough to hold
      > the first two lines. So when you issue the second ReadLine() you get the
      > second line. At some point StreamReader will read more bytes from the
      > underlying stream and decode them into its char buffer. Only then will it
      > re-read the first line.
      >
      > Lots of the System.IO reader writer classes employ buffering, making
      > manipulating the underlying streams tricky.
      >
      > David
      >
      >[/color]


      Comment

      Working...