title: basic_filebuf::underflow error reading the file with ifstream on /proc/pid/stat
body: <p>Why does the following code throw an exception? Note that the file is a <code>/proc/pid/stat</code> file so it could be interfered by the kernel.</p>
  
  <pre><code>// Checked that file does exist
  try {
    std::ifstream file(path.c_str());
    // Shouldn't even be necessary because it's the default but it doesn't 
    // make any difference.
    file.exceptions(std::ifstream::goodbit);
    // Read the stream into many fields
    // !!!! The exception was thrown here.
    file &gt;&gt; _ &gt;&gt; comm &gt;&gt; state &gt;&gt; ppid &gt;&gt; pgrp &gt;&gt; session &gt;&gt; tty_nr
         /* &gt;&gt; ... omitted */;
    file.close();
  } catch (const std::ifstream::failure&amp; e) {
    std::cout &lt;&lt; "Exception!!!! " &lt;&lt; e.what();
  }
  </code></pre>
  
  <p>The exception was "basic_filebuf::underflow error reading the file".</p>
  
  <p>Shouldn't the stream not throw an exception when we haven't asked it to (by setting       <code>file.exceptions()</code>)?</p>
  
  <p>More info:</p>
  
  <ul>
  <li>It runs on gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)</li>
  <li>The full code that causes the problem (without the try/catch part): <a href="https://github.com/apache/mesos/blob/e5c2edc3434b918bc9e25629b1571d7c283ce867/3rdparty/libprocess/3rdparty/stout/include/stout/proc.hpp#L214" rel="nofollow">proc.hpp</a></li>
  </ul>