Saturday, June 17, 2006 - 22:29

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:

15 Comments:

At 8/10/08 22:27, Anonymous Anonymous said...

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

 
At 22/12/08 23:31, Blogger Unknown said...

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.

 
At 28/1/09 09:10, Anonymous Anonymous said...

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!

 
At 25/3/09 19:14, Anonymous Anonymous said...

Should've been better documented in MSDN or perhaps I lack skills of searching thru MSDN. Nevertheless many thanks for posting.

 
At 29/7/09 18:51, Anonymous LeRoy said...

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

 
At 9/1/10 17:50, Anonymous Anonymous said...

Three years and a half later, your post is still useful.
Thanks a lot.

 
At 24/6/10 15:04, Anonymous Anonymous said...

still useful, thanks!

 
At 11/10/10 16:22, Blogger Unknown said...

Thanks man this post make me feel lucky. just like google...First link in the search page... peace.

 
At 2/12/10 19:52, Blogger Brennan said...

Hahah. Going to have to agree and say that this was incredibly helpful even going on four years later. Thanks bud.

 
At 15/12/10 17:55, Blogger Ganesh said...

thank you,i was struggling around that basestream and google (you) helped me..
pity everybody is thinking you as a 'dude' :)

 
At 8/3/11 18:34, Anonymous Anonymous said...

awesome!! super useful

 
At 8/3/12 19:51, Anonymous Shawn C said...

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!

 
At 24/5/12 19:00, Anonymous Anonymous said...

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!!!

 
At 25/8/12 10:56, Anonymous Anonymous said...

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

 
At 4/12/14 22:46, Anonymous Anonymous said...

Thank you! This is great and definitely useful.

 

Post a Comment

<< Home