Click to See Complete Forum and Search --> : StreamReader.BaseStream.Seek Problem


kohlimannu
April 24th, 2006, 08:17 AM
Hi all

in my application ive open a file in the streamreader and pass that streamreader to a function which runs in a loop.
in that function im changing the position of the cursor to the begining of the file everytime it loops in the functions but problem it move the cursor to first character and then when i try to read a line it skip the first character which makin a problem for me.
is ther any way for that


Private function SearchSubFunction(sr as streamreader)

sr.BaseStream.Seek(0, SeekOrigin.Begin)
sr.Read()

While sr.Peek() >= 0
single_line = sr.ReadLine.Trim
If single_line <> "" AndAlso single_line.StartsWith("'") = False AndAlso single_line.ToUpper.StartsWith("DIM") = False Then

'my code goes here
end if
end while

End Function

Craig Gemmill
April 25th, 2006, 01:13 PM
Get rid of that extra sr.Read(). It is causing the cursor to move one character forward.

kohlimannu
April 26th, 2006, 04:20 AM
Get rid of that extra sr.Read(). It is causing the cursor to move one character forward.
if i dont use that sr.read() than it wont go on first char its will be at the peek means cursor position will be -1 thats the problem thats why i put that sr.read

Thread1
April 26th, 2006, 07:00 AM
Private function SearchSubFunction(sr as streamreader)

sr.BaseStream.Seek(0, SeekOrigin.Begin)
sr.DiscardBufferedData()
'sr.Read()

While sr.Peek() >= 0
single_line = sr.ReadLine.Trim
If single_line <> "" AndAlso single_line.StartsWith("'") = False AndAlso single_line.ToUpper.StartsWith("DIM") = False Then

'my code goes here
end if
end while

End Function


the DiscardBufferedData() clears the buffer so that read methods will fetch the data from the basestream. for best performance in this situation, i would suggest shifting to BinaryStream.

kohlimannu
April 26th, 2006, 07:45 AM
Private function SearchSubFunction(sr as streamreader)

sr.BaseStream.Seek(0, SeekOrigin.Begin)
sr.DiscardBufferedData()
'sr.Read()

While sr.Peek() >= 0
single_line = sr.ReadLine.Trim
If single_line <> "" AndAlso single_line.StartsWith("'") = False AndAlso single_line.ToUpper.StartsWith("DIM") = False Then

'my code goes here
end if
end while

End Function


the DiscardBufferedData() clears the buffer so that read methods will fetch the data from the basestream. for best performance in this situation, i would suggest shifting to BinaryStream.
Thanx Thread1

its working cool

Thread1
April 26th, 2006, 07:50 AM
sure kohlimannu, glad to know that it is working now :)