Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Bottom-line top:</strong> With proper handling of white-space, the following is how <code>eof</code> can be used (and even, be more reliable than <code>fail()</code> for error checking):</p> <pre><code>while( !(in&gt;&gt;std::ws).eof() ) { int data; in &gt;&gt; data; if ( in.fail() ) /* handle with break or throw */; // now use data } </code></pre> <p>(<em>Thanks Tony D for the suggestion to highlight the answer. See his comment below for an example to why this is more robust.</em>)</p> <hr> <p>The main argument against using <code>eof()</code> seems to be missing an important subtlety about the role of white space. My proposition is that, checking <code>eof()</code> explicitly is not only not "<em>always wrong</em>" -- which seems to be an overriding opinion in this and similar SO threads --, but with proper handling of white-space, it provides for a cleaner and more reliable error handling, and is the <em>always correct</em> solution (although, not necessarily the tersest).</p> <p>To summarize what is being suggested as the "proper" termination and read order is the following:</p> <pre><code>int data; while(in &gt;&gt; data) { /* ... */ } // which is equivalent to while( !(in &gt;&gt; data).fail() ) { /* ... */ } </code></pre> <p>The failure due to read attempt beyond eof is taken as the termination condition. This means is that there is no easy way to distinguish between a successful stream and one that really fails for reasons other than eof. Take the following streams: </p> <ul> <li><code>1 2 3 4 5&lt;eof&gt;</code></li> <li><code>1 2 a 3 4 5&lt;eof&gt;</code> </li> <li><code>a&lt;eof&gt;</code></li> </ul> <p><code>while(in&gt;&gt;data)</code> terminates with a set <code>failbit</code> for <em>all</em> three input. In the first and third, <code>eofbit</code> is also set. So past the loop one needs very ugly extra logic to distinguish a proper input (1st) from improper ones (2nd and 3rd).</p> <p>Whereas, take the following: </p> <pre><code>while( !in.eof() ) { int data; in &gt;&gt; data; if ( in.fail() ) /* handle with break or throw */; // now use data } </code></pre> <p>Here, <code>in.fail()</code> verifies that as long as there is something to read, it is the correct one. It's purpose is not a mere while-loop terminator. </p> <p>So far so good, but what happens if there is trailing space in the stream -- what sounds like the major concern against <code>eof()</code> as terminator?</p> <p>We don't need to surrender our error handling; just eat up the white-space:</p> <pre><code>while( !in.eof() ) { int data; in &gt;&gt; data &gt;&gt; ws; // eat whitespace with std::ws if ( in.fail() ) /* handle with break or throw */; // now use data } </code></pre> <p><code>std::ws</code> skips any potential (zero or more) trailing space in the stream while setting the <code>eofbit</code>, and <strong>not the <code>failbit</code></strong>. So, <code>in.fail()</code> works as expected, as long as there is at least one data to read. If all-blank streams are also acceptable, then the correct form is:</p> <pre><code>while( !(in&gt;&gt;ws).eof() ) { int data; in &gt;&gt; data; if ( in.fail() ) /* handle with break or throw */; /* this will never fire if the eof is reached cleanly */ // now use data } </code></pre> <p><strong>Summary:</strong> A properly constructed <code>while(!eof)</code> is not only possible and not wrong, but allows data to be localized within scope, and provides a cleaner separation of error checking from business as usual. That being said, <code>while(!fail)</code> is inarguably a more common and terse idiom, and may be preferred in simple (single data per read type of) scenarios. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload