end of file

  • Thread starter Thread starter hu
  • Start date Start date
H

hu

How to figure out a stream is at the End-of-file? In C, just check the FILE*
equals to null. But how to do it in C#?
I know through StreamReader.ReadLine(), I can know when I reach the end of
file. However, this method is not suitable because I do not want to read a
line first... Any comments? Ideas?

Thanks,

HU
 
Bit more of a story would be:

Not all streams support peeking, seeking, etc... You can not always tell if
you are at the end of a stream. If you are using a FileStream then you
should use fileStream.Position == fileStream.Length to see if you are at the
end. If you are using a StreamReader you should get access to BaseStream,
make sure the CanSeek property returns true, and then check the Position
versus the Length. Simply doing a Peek can have negative side effects, such
as loading more data from disk since a StreamReader is buffered. If the
buffer is exhausted then you'll have to reload the buffer before it can
check the next char making Peek behave at an indeterminate speed.

Of course most of this really doesn't matter unless your coding some form of
high performance application.
 
Back
Top