Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>-1 is <code>get</code>'s way of saying you've reached the end of file. Compare it using the <code>std::char_traits&lt;char&gt;::eof()</code> (or <code>std::istream::traits_type::eof()</code>) - avoid -1, it's a magic number. (Although the other one is a bit verbose - you can always just call <code>istream::eof</code>)</p> <p>The EOF flag is only set once a read tries to read <em>past the end of the file</em>. If I have a 3 byte file, and I only read 3 bytes, EOF is <code>false</code>, because I've not tried to read past the end of the file yet. While this seems confusing for files, which typically know their size, EOF is not known until a read is attempted on some devices, such as pipes and network sockets.</p> <p>The second example works as <code>inf &gt;&gt; foo</code> will always return <code>inf</code>, with the side effect of attempt to read something and store it in <code>foo</code>. <code>inf</code>, in an <code>if</code> or <code>while</code>, will evaluate to <code>true</code> if the file is "good": no errors, no EOF. Thus, when a read fails, <code>inf</code> evaulates to <code>false</code>, and your loop properly aborts. However, take this common error:</p> <pre><code>while(!inf.eof()) // EOF is false here { inf &gt;&gt; x; // read fails, EOF becomes true, x is not set // use x // we use x, despite our read failing. } </code></pre> <p>However, this:</p> <pre><code>while(inf &gt;&gt; x) // Attempt read into x, return false if it fails { // will only be entered if read succeeded. } </code></pre> <p>Which is what we want.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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