Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is very easy to differentiate between EOF and other errors, as long as you don't configure the stream to use exceptions.</p> <p>Simply check <code>stream.eof()</code> at the end.</p> <p>Before that only check for failure/non-failure, e.g. <code>stream.fail()</code> or <code>!stream</code>. Note that <code>good</code> is not the opposite of <code>fail</code>. So in general never even look at the <code>good</code>, only at the <code>fail</code>.</p> <hr> <p><strong>Edit:</strong></p> <p>Some example code, namely your example modified to distinguish an ungood bool specification in the data:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; #include &lt;stdexcept&gt; using namespace std; bool throwX( string const&amp; s ) { throw runtime_error( s ); } bool hopefully( bool v ) { return v; } bool boolFrom( string const&amp; s ) { istringstream stream( s ); (stream &gt;&gt; boolalpha) || throwX( "boolFrom: failed to set boolalpha mode." ); bool result; (stream &gt;&gt; result) || throwX( "boolFrom: failed to extract 'bool' value." ); char c; stream &gt;&gt; c; hopefully( stream.eof() ) || throwX( "boolFrom: found extra characters at end." ); return result; } void readbools( istream&amp; is ) { string word; while( is &gt;&gt; word ) { try { bool const b = boolFrom( word ); cout &lt;&lt; (b ? "T" : "F") &lt;&lt; endl; } catch( exception const&amp; x ) { cerr &lt;&lt; "!" &lt;&lt; x.what() &lt;&lt; endl; } } cout &lt;&lt; "- " &lt;&lt; is.good() &lt;&lt; is.eof() &lt;&lt; is.fail() &lt;&lt; is.bad() &lt;&lt; "\n"; } void testread( string const&amp; s ) { istringstream is( s ); readbools( is ); } int main() { cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false" ); cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false tr" ); cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false truex" ); } </code></pre> <p>Example result:</p> <pre> ------------------------------------------------------------ T F - 0110 ------------------------------------------------------------ T F !boolFrom: failed to extract 'bool' value. - 0110 ------------------------------------------------------------ T F !boolFrom: found extra characters at end. - 0110 </pre> <hr> <p><strong>Edit 2</strong>: in the posted code and results, added example of using <code>eof()</code> checking, which I forgot.</p> <hr> <p><strong>Edit 3</strong>: The following corresponding example uses the OP&rsquo;s proposed skip-whitespace-before-reading solution:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; using namespace std; void readbools( istream&amp; is ) { bool b; while( is &gt;&gt; ws &amp;&amp; !is.eof() &amp;&amp; is &gt;&gt; b ) // &lt;- Proposed scheme. { cout &lt;&lt; (b ? "T" : "F") &lt;&lt; endl; } if( is.fail() ) { cerr &lt;&lt; "!readbools: failed to extract 'bool' value." &lt;&lt; endl; } cout &lt;&lt; "- " &lt;&lt; is.good() &lt;&lt; is.eof() &lt;&lt; is.fail() &lt;&lt; is.bad() &lt;&lt; "\n"; } void testread( string const&amp; s ) { istringstream is( s ); is &gt;&gt; boolalpha; readbools( is ); } int main() { cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false" ); cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false tr" ); cout &lt;&lt; string( 60, '-' ) &lt;&lt; endl; testread( "true false truex" ); } </code></pre> <p>Example result:</p> <pre> ------------------------------------------------------------ T F - 0100 ------------------------------------------------------------ T F !readbools: failed to extract 'bool' value. - 0110 ------------------------------------------------------------ T F T !readbools: failed to extract 'bool' value. - 0010 </pre> <p>The main difference is that this approach produces 3 successfully read values in the third case, even though the third value is incorrectly specified (as <code>"truex"</code>).</p> <p>I.e. it fails to recognize an incorrect specification as such.</p> <p>Of course, my ability to write Code That Does Not Work&trade; is no proof that it can not work. But I am fairly good at coding up things, and I could not see any way to detect the <code>"truex"</code> as incorrect, with this approach (while it was easy to do with the read-words exception based approach). So at least for me, the read-words exception based approach is simpler, in the sense that it is easy to make it behave correctly.</p> <p>Cheers &amp; hth.,</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