end of file

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
 
J

Justin Rogers

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top