C# checking for binary reader end of file
<p>I was searching for a way to check whether I've reached the end of a file for my binary reader and one suggestion was to use PeekChar as such</p>

<pre><code>while (inFile.PeekChar() &gt; 0)
{
    ...
}
</code></pre>

<p>However, it looks like I've run into an issue</p>

<pre>
Unhandled Exception: System.ArgumentException: The output char buffer is too sma
ll to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'Syste
m.Text.DecoderReplacementFallback'.
Parameter name: chars
   at System.Text.Encoding.ThrowCharsOverflow()
   at System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothin
gDecoded)
   at System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char* char
s, Int32 charCount, DecoderNLS baseDecoder)
   at System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars,
 Int32 charCount, Boolean flush)
   at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteC
ount, Char[] chars, Int32 charIndex, Boolean flush)
   at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteC
ount, Char[] chars, Int32 charIndex)
   at System.IO.BinaryReader.InternalReadOneChar()
   at System.IO.BinaryReader.PeekChar()
</pre>

<p>So maybe PeekChar isn't the best way to do it, and I don't think it should even be used that way because I'm checking the current position of my reader and not really what the next character is supposed to be.</p>

<p>EDIT:</p>

<p>Here's a working solution that sort of fits the logic I had in mind. Didn't know there was a BaseStream object to work with.</p>

<pre><code>while (inFile.BaseStream.Position() != inFile.BaseStream.Length())
{
   ...
}
</code></pre>

<p>Check the current position and size of the file directly.</p>
