How does does ifstream eof() work?
<pre><code>#include &lt;vector&gt;
#include &lt;list&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;deque&gt;
#include &lt;stack&gt;
#include &lt;bitset&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;
#include &lt;numeric&gt;
#include &lt;utility&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;cstdio&gt;
#include &lt;cmath&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;
#include &lt;cctype&gt;
#include &lt;fstream&gt;

using namespace std;

int main() {
    fstream inf( "ex.txt", ios::in );
    while( !inf.eof() ) {
        std::cout &lt;&lt; inf.get() &lt;&lt; "\n";
    }
    inf.close();
    inf.clear();
    inf.open( "ex.txt", ios::in );
    char c;
    while( inf &gt;&gt; c ) {
        std::cout &lt;&lt; c &lt;&lt; "\n";
    }
    return 0;
}
</code></pre>

<p>I'm really confused about eof() function. Suppose my ex.txt's content is:</p>

<pre><code>abc
</code></pre>

<p>It always reads an extra character and show -1. when reading using eof()? But the inf >> c gave the correct output which was 'abc'? Can anyone help me explain this?</p>

<p>Best regards, 
Chan Nguyen</p>
