Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>tellg()</code> function works by attempting to construct a sentry object and then checking for the <code>failbit</code> before returning a proper value. If the <code>failbit</code> is set, it returns -1. Details can be found <a href="http://www.cplusplus.com/reference/istream/istream/tellg/" rel="nofollow">here</a> or, if you prefer a more official source and don't mind a dry read, the ISO C++ standard (<code>27.6.1.3/36a-37</code> for C++03, <code>27.7.2.3/39-40</code> for C++11).</p> <p>The construction of the sentry first checks any of the <a href="http://www.cplusplus.com/reference/ios/ios/rdstate/" rel="nofollow">error flags</a> (like <code>eofbit</code>) and, if set, it sets the <code>failbit</code> and returns. See <a href="http://www.cplusplus.com/reference/istream/istream/sentry/" rel="nofollow">here</a> for detail (C++03 <code>27.6.1.1.2</code>, C++11 <code>27.7.2.1.3</code>),</p> <p>Hence a <code>tellg()</code> after the end of file flag has been set will fail. The fact that you're reading lines until <code>getline</code> returns false means that the stream's <code>eofbit</code> is being set, hence you've reached the end of the file.</p> <p>You can see the behavior with this following program:</p> <pre><code>#include &lt;iostream&gt; #include &lt;iomanip&gt; int main (void) { std::string line; while (std::getline (std::cin, line)) { if (line.length() &gt; 20) line = line.substr(0,17) + "..."; std::cout &lt;&lt; "tellg() returned " &lt;&lt; std::setw(5) &lt;&lt; std::cin.tellg() &lt;&lt; " after " &lt;&lt; line &lt;&lt; "\n"; } //std::cin.clear(); std::cout &lt;&lt; "tellg() returns: " &lt;&lt; std::cin.tellg() &lt;&lt; '\n'; return 0; } </code></pre> <p>When you run that and provide the file itself as input, you see:</p> <pre><code>tellg() returned 20 after #include &lt;iostream&gt; tellg() returned 39 after #include &lt;iomanip&gt; tellg() returned 40 after tellg() returned 58 after int main (void) { tellg() returned 80 after std::string l... tellg() returned 124 after while (std::g... tellg() returned 156 after if (line.... tellg() returned 202 after line ... tellg() returned 243 after std::cout... tellg() returned 291 after &lt;&lt; st... tellg() returned 333 after &lt;&lt; " ... tellg() returned 339 after } tellg() returned 363 after //std::cin.cl... tellg() returned 400 after std::cout &lt;&lt; ... tellg() returned 437 after &lt;&lt; std::c... tellg() returned 451 after return 0; tellg() returned 453 after } tellg() returned 454 after tellg() returns: -1 </code></pre> <p>If you uncomment the line in that code which clears the error state variables, it will work:</p> <pre><code>tellg() returned 20 after #include &lt;iostream&gt; tellg() returned 39 after #include &lt;iomanip&gt; tellg() returned 40 after tellg() returned 58 after int main (void) { tellg() returned 80 after std::string l... tellg() returned 124 after while (std::g... tellg() returned 156 after if (line.... tellg() returned 202 after line ... tellg() returned 243 after std::cout... tellg() returned 291 after &lt;&lt; st... tellg() returned 333 after &lt;&lt; " ... tellg() returned 339 after } tellg() returned 361 after std::cin.clea... tellg() returned 398 after std::cout &lt;&lt; ... tellg() returned 435 after &lt;&lt; std::c... tellg() returned 449 after return 0; tellg() returned 451 after } tellg() returned 452 after tellg() returns: 452 </code></pre> <hr> <p>And, as an aside, it looks like the bug you're referring to may be <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2259" rel="nofollow">this one</a> (it's a little unclear since the post you linked to is sadly missing any detail - it would have been better had the poster bothered to <em>support</em> his assertion that it was a known bug by, for example, <em>linking</em> to it).</p> <p>If that's the case, the <em>first</em> thing you should notice is that it was fixed more than a decade ago so, unless you're using an absolutely <em>ancient</em> <code>gcc</code>, it's not going to be an issue now.</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