Streamreader - Can I avoid buffering?

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

    #1

    Streamreader - Can I avoid buffering?

    Hi.
    I am tring to read from random parts of an SDF file using a stream
    reader. I know each line in my file is 30 chars long so I know where in
    the file I want to go to read a specific record.
    The first seek works fine. But due to buffering (from what I've read)
    the second seek will not point to the start of the file and will be
    positioned just 30 bytes after the first seek. Is there any way I can
    avoid the buffering or is there another solution that would allow me to
    read from any part of a text file?

    Any help/advice would be great.


    My code.

    Dim bytes(29) As Char
    Dim bytes2(29) As Char

    Dim fs As New FileStream("h:\ address_procV1\ names.sdf", fileMode.Open,
    FileAccess.Read )
    ' Create a character reader.
    Dim sr As New StreamReader(fs )


    sr.BaseStream.S eek(21657360, SeekOrigin.Begi n)
    '''sr.ReadBlock (bytes2, 0, 30)
    sr.Read(bytes, 0, 30)
    sr.BaseStream.S eek(0, SeekOrigin.Begi n)
    '''sr.ReadBlock (bytes2, 0, 30)
    sr.Read(bytes2, 0, 30)
  • Chris Dunaway

    #2
    Re: Streamreader - Can I avoid buffering?

    You can call the .DiscardBuffere dData method of the StreamReader before
    you next seek and that should work.

    Comment

    • dave

      #3
      Re: Streamreader - Can I avoid buffering?

      Chris Dunaway wrote:[color=blue]
      > You can call the .DiscardBuffere dData method of the StreamReader before
      > you next seek and that should work.
      >[/color]

      Thanks a million Chris. Works as I want now.
      Spent so long looking at it I didn't even notice the .DiscardBuffere dData


      Cheers
      Dave

      Comment

      Working...