Tiger said:
Brian Brown,
My code is like this:
FileStream fs;
fs=new FileStream("cc.txt",FileMode.Open,FileAccess.Read);
StreamReader r=new StreamReader(fs);
Console.WriteLine((char)r.Peek());
//r.DiscardBufferedData();
r.BaseStream.Seek(5,SeekOrigin.Current);
//r.DiscardBufferedData();
Console.WriteLine((char)r.Peek());
<snip>
Just to explain in a slightly different way to Brian (maybe).
When you first call r.Peek(), the StreamReader is reading a chunk of
data from the stream. That leaves the stream positioned later in the
data than you'd expect - at the end of the file, in your particular
case. You're then telling the stream to reposition itself 5 bytes later
- i.e. still at the end of the file. You're then telling the
StreamReader to discard the buffered data (assuming you've removed the
comment), and it then tries to read from the stream, and finds out it's
at the end of the file.
So, if you change your SeekOrigin.Current to SeekOrigin.Begin you'll
end up with
a
f
instead. It does mean you need to keep track of where you want to go to
rather than using SeekOrigin.Current, unfortunately - if you just want
to skip five characters, then using StreamReader.Read (remembering to
use the return value to find out how many characters you've actually
read) is probably easier.