title: Using getline to read from text file leads to infinite loop when check is eof
body: <p>I am trying to get some data from a text file that are in specific lines (1st, 7th, 13th, etc - data needed is placed at the next 6th-line)</p>
  
  <p>My code so far is this:</p>
  
  <pre><code>txtfile = "titles.txt";
  ifstream txt(txtfile);
  const int buffer_size = 80;
  char title_buffer[buffer_size];
  const int titleLineDiff = 6;
  if (txt.is_open())
  {
      while(!txt.eof())
      {
          static int counter = 1;
          txt.getline(title_buffer, buffer_size);
          cout &lt;&lt; "Title: \"" &lt;&lt; counter &lt;&lt; "." &lt;&lt; title_buffer &lt;&lt; "\"" &lt;&lt; endl;
          counter++;
          //seek to the next title...difference is 6 lines
          for(int i = 0; i &lt; titleLineDiff; i++)
              txt.getline(title_buffer, 40);
      }
  }
  </code></pre>
  
  <p>Now, it works fine with this file I've created:</p>
  
  <pre><code>testONE
  two
  three
  four 
  five
  six
  
  testTWO
  bla
  </code></pre>
  
  <p>And it prints "testONE" and "testTWO" but when I am trying to open the file that contains the data, I get an infinite loop and the output is </p>
  
  <blockquote>
    <p>Title: "counter_increasing_number."</p>
  </blockquote>
  
  <p>The text document, was copied from the internet and this might be the cause of the problem in reading it.</p>
  
  <p>What can I do about this?</p>
  
  <hr>
  
  <p>I've changed the code to this:</p>
  
  <pre><code>while(getline(txt,title_buffer))
  {
      static int counter = 1;
      //getline(title_buffer, buffer_size);
      cout &lt;&lt; "Title: \"" &lt;&lt; counter &lt;&lt; "." &lt;&lt; title_buffer &lt;&lt; "\"" &lt;&lt; endl;
      counter++;
      //seek to the next title...difference is 6 lines
      for(int i = 0; i &lt; titleLineDiff; i++)
      {
          getline(txt, title_buffer);
      }
  }
  </code></pre>
  
  <p>and it worked. </p>
  
  <p>Could somebody explain me the reason why the first didn't work?</p>