StreamReader ReadLine corruption after Seek(0,0) Framework 1.0 sp2

  • Thread starter Thread starter Chris B
  • Start date Start date
C

Chris B

I get a corrupted line using ReadLine after using the Seek method.
I have easily rewritten it to not use the Seek call, but this kind of event
is worrying.
(Yes I know reading the file twice is not very smart, but the code is
designed to only run
a few times and this section is non critical -- using this solution appeared
to be the simplest
and less error prone )
System.IO.StreamReader srReadLine = new System.IO.StreamReader(


(System.IO.Stream)System.IO.File.OpenRead(fileInfo.FullName),System.Text.Enc
oding.ASCII);

srReadLine.BaseStream.Seek(0,System.IO.SeekOrigin.Begin); //This seek is OK

// Look for some record buried in the file

while (srReadLine.Peek() > -1)

{

// find some record, if found then break else throw exception.

row = srReadLine.ReadLine();

}

srReadLine.BaseStream.Seek(0,System.IO.SeekOrigin.Begin); //This seek leads
to the ReadLine corrupting

while (srReadLine.Peek() > -1)

{

//If the Seek above is used then the ReadLine will be corrupted as the
Stream gets neaar the end of the file

// Reads about 50 lines OK. Then 5 lines from the end ReadLine returns half
of that line

// and most of the final line in that file!!!!!!

row = srReadLine.ReadLine();

}
 
Chris B said:
I get a corrupted line using ReadLine after using the Seek method.
I have easily rewritten it to not use the Seek call, but this kind of event
is worrying.

Not really. StreamReader can buffer input - it gets confused if you
seek underneath its feet. I'm surprised at exactly how the error is
occuring though.

Have you tried calling StreamReader.DiscardBufferedData after the seek
but before the next read?
\--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Jon Skeet said:
Not really. StreamReader can buffer input - it gets confused if you
seek underneath its feet. I'm surprised at exactly how the error is
occuring though.

Have you tried calling StreamReader.DiscardBufferedData after the seek
but before the next read?

No, as I have rewritten the code. OK DiscardBufferedData may work, but I
would have thought that performing a Seek would implicitly call this
DiscardBufferedData method. However my lesson is learned to not use dumb
code.

If you are really interested I can post the source code and data file in
order to (perhaps) reproduce this error.

Thanks for the reply
 
Chris B said:
No, as I have rewritten the code. OK DiscardBufferedData may work, but I
would have thought that performing a Seek would implicitly call this
DiscardBufferedData method. However my lesson is learned to not use dumb
code.

How could Seek call DiscardBufferedData? You were seeking on the *base*
stream, which didn't know there was a StreamReader on top of it.
If you are really interested I can post the source code and data file in
order to (perhaps) reproduce this error.

I would be interested to have a look, yes - but only if it's fairly
easy for you to resurrect the code/data.
 
Back
Top