title: eof of istream in C++
body: <pre><code>bool ios::eof ( ) const;
  </code></pre>
  
  <p>According to the library,</p>
  
  <blockquote>
    <p>The function returns true if the eofbit stream's error flag has been
    set by a previous i/o operation. This flag is set by all standard
    input operations when the End Of File is reached in the sequence
    associated with the stream.</p>
  </blockquote>
  
  <p>I wrote a program to run some tests:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      ifstream ifs(argv[1]);
      float f;
      ifs &gt;&gt; f;
  
      cout &lt;&lt; ifs.eof() &lt;&lt; endl; //check if eofbit set
  
      ifs.close();
  }
  </code></pre>
  
  <p>I tested 2 files, testcase1.txt and testcase2.txt.</p>
  
  <p>testcase1.txt was generated in the terminal with <code>cat</code>, [Ctrl-D] was used to end input:</p>
  
  <pre><code>[~/C++ $] cat &gt; testcase1.txt
  1.234[Ctrl-D]
  </code></pre>
  
  <p>testcase2.txt was generated in <code>vim</code>, I opened up <code>vim</code> and just inputted <code>1.234</code>, and then saved and exited.</p>
  
  <p><strong>Test Result</strong></p>
  
  <p>Test result with <code>testcase1.txt</code> is <code>1</code>, which means the <code>eofbit</code> is set,</p>
  
  <pre><code>[~/C++ $] ./a.out testcase1.txt
  1
  </code></pre>
  
  <p>Test result with <code>testcase2.txt</code> is <code>0</code>,</p>
  
  <pre><code>[~/C++ $] ./a.out testcase2.txt
  0
  </code></pre>
  
  <p>I open both <code>testcase1.txt</code> and <code>testcase2.txt</code> in <code>vim</code>, they look exactly the same, then why the <code>eofbit</code> wasn't set for <code>testcase2.txt</code>?</p>