By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Streamreader - Can I avoid buffering?

P: n/a
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)
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You can call the .DiscardBufferedData method of the StreamReader before
you next seek and that should work.

Nov 21 '05 #2

P: n/a
Chris Dunaway wrote:
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
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.