Streamreader - Can I avoid buffering?

D

dave

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.Seek(21657360, SeekOrigin.Begin)
'''sr.ReadBlock(bytes2, 0, 30)
sr.Read(bytes, 0, 30)
sr.BaseStream.Seek(0, SeekOrigin.Begin)
'''sr.ReadBlock(bytes2, 0, 30)
sr.Read(bytes2, 0, 30)
 
C

Chris Dunaway

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

dave

Chris said:
You can call the .DiscardBufferedData method of the StreamReader before
you next seek and that should work.

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


Cheers
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top