title: Testing stream.good() or !stream.eof() reads last line twice
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong">Why is iostream::eof inside a loop condition considered wrong?</a>  </p>
  </blockquote>
  
  
  
  <p>I have the following piece of code:</p>
  
  <pre><code>ifstream f("x.txt");
  string line;
  while (f.good()) {
    getline(f, line);
    // Use line here.
  }
  </code></pre>
  
  <p>But this reads the last line twice.  Why does this happen and how do I fix it?</p>
  
  <p>Something very similar happens with:</p>
  
  <pre><code>ifstream f("x.txt");
  string line;
  while (!f.eof()) {
    getline(f, line);
    // Use line here.
  }
  </code></pre>