473,513 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.VisualBasic
Sub Main1()
Dim oFileStream As FileStream
oFileStream = New FileStream("c:myfile.txt",
FileMode.Open,FileAccess.Read)
Dim oReader As StreamReader
oReader = New StreamReader(oFileStream)
Dim sString1 As String
sString 1 = oReader.ReadLine()
oFileStream .Seek(0, SeekOrigin.Begin)
Dim sString2 = oReader.ReadLine()
MsgBox(sString1, sString2)
End Sub

Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Sub Main1()
Dim oFileStream As FileStream
oFileStream = New FileStream("c:myfile.txt",
FileMode.Open,FileAccess.Read)
Dim oReader As StreamReader
oReader = New StreamReader(oFileStream)
Dim sString1 As String
sString 1 = oReader.ReadLine()
oFileStream .Seek(0, SeekOrigin.Begin)
oReader = New StreamReader(oFileStream)
Dim sString2 = oReader.ReadLine()
MsgBox(sString1, sString2)
End Sub
Nov 21 '05 #1
2 7705

"Joan Reddy" <tj*****@earthlink.net> wrote in message
news:Dn***************@newsread3.news.atl.earthlin k.net...
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??


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
Nov 21 '05 #2
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" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...

"Joan Reddy" <tj*****@earthlink.net> wrote in message
news:Dn***************@newsread3.news.atl.earthlin k.net...
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??


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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12556
by: Harry J. Smith | last post by:
In the code below the two lines marked with ????????????????? do not work properly. The line: inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to beginning of the file...
11
35700
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
1
18359
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
7
22381
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
2
1720
by: dave | last post by:
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...
0
917
by: rjjaw | last post by:
I need help with the streamreader. It appears that when I am reading the file that the streamreader jumps 3 lines before it is picked up by my variable again. Therefore I am only capturing a...
9
5070
by: lord.zoltar | last post by:
Hi, I'm having some trouble with a StreamReader. I use a System.IO.StreamReader to read a text file and then print it. If the user has selected a range of pages starting past the first page, I...
1
6276
by: gandinarender | last post by:
hello every one,i have used a stream reader to read a file, but after the compilation i am not able to get the text in the file as it is.for example i am retrieving a .doc file(ex:resume) and when i...
4
17890
by: somequestion | last post by:
Question 1. i am using StreamReader Class like this... string str = string.Empty; StreamReader sr = new StreamReader(fsIn,Encoding.Default) while ((str = sr.ReadLine()) != null) { // wanna get...
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.