Rewinding a TextReader file stream.

  • Thread starter Thread starter info
  • Start date Start date
I

info

Hi All,


How can i rewind the following sile stream:

TextReader tr = new StreamReader(File.Open(fileName, FileMode.Open));

Is there a dedicated method or shall I close and re-open the file...


Thanks a lot,

Alberto
 
Thanks guys,


Can you please provide me a small speudocode example of opening a text
file/reading from it/rewind it/read again?


Thanks,

Alberto
 
I don't see any rewind in that example...

The example for the Seek method rewinds the stream back to the
beginning.

Personally I prefer to use the Position property, mind you...
 
Kevin,

I cannot read a text file line with the FileStream class. The Read
method wants to know before reading how many bytes to read...

I need the StremReader.ReadLine method...

How can I proceed?


Thanks again,

Alberto
 
I don't know what you mean by "I cannot read a text file line with the
FileStream class." You can't use a method that reads each line, no, but you
can certainly identify line breaks. So, yes, you have to do a bit more work
in terms of your code, breaking the lines yourself.

Here's the deal: If you use a StreamReader/TextReader to read the file, you
can only move forward. To go backward, you would have to close and re-open
the StreamReader. That is going to be time-consuming, in terms of
performance. If you use a FileStream you have random access to the file, but
you have to manage the data you read in your code. The buffer that you read
into is just a temporary storage container, which you can re-use throughout
the lifetime of the FileStream. So, you can identify line breaks and build
your own strings from the data you read.

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
 
Thanks a lot Kevin, so I need to write a method that read from the
stream and stop once found a line break and create a string....

I will think about it...
 
Back
Top