Resetting a StreamReader
I've been doing a lot of personal posts lately, so here's a techie one for a change.
Firstly, an article on El Reg about why Bill Gates isn't quite the saint that everyone's currently making him out to be.
Secondly, though, a useful piece of code that I'll probably lose if I don't post it here. Sometimes it's necessary to reset a file pointer; you've read a bit of the file, but you need to go back to the beginning again. With most languages it's pretty straightforward, but with C# it depends on what kind of stream you're using. With most streams you can just seek to the beginning of the file, or set the position of the stream to 0. When using a StreamReader, though, you need to do both of those and discard the data that the stream has already read in and is holding in its cache:
fileReader.DiscardBufferedData();
fileReader.BaseStream.Seek(0, SeekOrigin.Begin);
fileReader.BaseStream.Position = 0;
Still pretty simple to do, but it's easy to forget the cache, or just seek to the beginning and forget to set the position to 0.
Labels: Coding
15 Comments:
Dude, your post was SUPER useful i was this close to giving up on getting my StreamReaders pointer to go back... and its for a project for a potential job!! Shot man.
From a fellow Capetown programmer
Thanks man, that was a real life saver. For my work I needed to combine two files together, and I'm not much of a programmer, never dealt with text files before. I knew what the problem was, but even closing and opening the stream didn't seem to clear it and I had no idea what else to do.
Props.
Unbelievable... during debugging I could just reset the position... why do I need 3 lines of code to do that?
tnx a lot, really helped me out!
Should've been better documented in MSDN or perhaps I lack skills of searching thru MSDN. Nevertheless many thanks for posting.
Hey, you can always extend the streamreader class and add a Reset method.. it's a c++-like language, right?
Of course I'm too lazy to do that but if I were a proper code librarian..
Excellently useful post, thank you
Three years and a half later, your post is still useful.
Thanks a lot.
still useful, thanks!
Thanks man this post make me feel lucky. just like google...First link in the search page... peace.
Hahah. Going to have to agree and say that this was incredibly helpful even going on four years later. Thanks bud.
thank you,i was struggling around that basestream and google (you) helped me..
pity everybody is thinking you as a 'dude' :)
awesome!! super useful
Thanks a million CJ, works like a charm, am converting >130K lines of Delphi to PRISM and I was STUCK. Good one. Hope someone does you a biiig favour. Soon!
WOW you saved me. Just started a new job, working on a new project. I spent HOURS trying to think my way out of this problem... I never would have thought to use those three lines.
THANKS A MILLION!!!
4 guys working in VC++ 2010 with StreamReader class:
FilePointer -> DiscardBufferedData();
FilePointer -> BaseStream -> Seek(0,SeekOrigin::Begin);
It returns the pointer to the origin of the file...
Hav phun :P
Thank you! This is great and definitely useful.
Post a Comment
<< Home