getline() sets failbit and skips last line
<p>I'm using <code>std::getline()</code> to enumerate through the lines in a file, and it's mostly working. It's left me curious however - <code>std::getline()</code> is skipping the very last line in my file, but only if it's blank. Using this minimal example:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{
        std::string line;
        while(std::getline(std::cin, line))
                std::cout &lt;&lt; "Line: “" &lt;&lt; line &lt;&lt; "”\n";
        return 0;
}
</code></pre>

<p>If I feed it this:</p>

<pre><code>Line A
Line B
Line C
</code></pre>

<p>I get those lines back at me. But this:</p>

<pre><code>Line A
Line B
Line C
[* line is present but blank, ie, the file end is: "...B\nLine C\n" *]
</code></pre>

<p>(I unfortunately can't have a blank line in SO's little code box thing...)
So, first file has three lines ( ["Line A", "Line B", "Line C"] ), second file has four ( ["Line A", "Line B", "Line C", ""] )</p>

<p>This to me seems wrong - I have a four line file, and enumerating it with getline() leaves me with 3. What's really got me scratching my head is that this is exactly what the standard says it should do. (21.3.7.9)</p>

<p>Even Python has similar behaviour (but it gives me the newlines too - C++ chops them off.) Is this some weird thing where C++ is expected lines to be terminated, and not separated by '\n', and I'm feeding it differently?</p>

<h2>Edit</h2>

<p>Clearly, I need to expand a bit here. I've met up with two philosophies of determining what a "line" in a file is:</p>

<ul>
<li><strong>Lines are <em>terminated</em> by newlines</strong> - Dominant in systems such as Linux, and editors like vim. Possible to have a slightly "odd" file by not having a final '\n' (a "noeol" in vim). Impossible to have a blank line at the end of a file.</li>
<li><strong>Lines are <em>separated</em> by newlines</strong> - Dominant in just about every Windows editor I've ever come across. Every file is valid, and it's possible to have the last line be blank.</li>
</ul>

<p>Of course, YMMV as to what a newline is.</p>

<p>I've always treated these as two completely different schools of thought. One earlier point I tried to make was to ask if the C++ standard was explicitly or merely implicitly following the first.</p>

<p>Thus, getting back to the question at hand, the second example, which can be thought of as "A\nB\nC\n" has <em>four</em> lines, following the separated philosophy. Now, does C++ explicitly follow a terminated philosophy, or is this just the way the standard is? (They don't record much reasoning in standards...) I'm hesitant to say it was explicit, since it's a bit painful to tell if you have what vim calls a "noeol" file with C++. (Python, for example, leaves the newlines in, so you can tell that way)</p>

<p>Since everything in Windows follows the separated philosophy, I'm looking for something a bit deeper than "Both examples have 3 lines."</p>

<p>(Curiously, where is Mac? terminated or separated?)</p>
