title: C++ stringstreams, why getline does not consume content of stream?
body: <p>I found a odd behavior concerning stringstreams and getline [C++].</p>
  
  <p>What am I doing?</p>
  
  <p>In normal operation I stuff strings for logging into a stringstream, which I use as a simple self scaling buffer. Then on certain events I use getline to extract complete lines from the buffer-stream filter em for certain criteria and if they match I put such lines to one or more output streams for generating log output (on screen, to files, you name it...). In Case of termination of my programm - normal or abnormal, I want to grab what is left in the buffer (incomplete line[s]) an also put that to the output streams. For that I thought the normal str() method of stringstream will do the job.</p>
  
  <p>unfortunately it turns out that getline seems to not consume the lines extracted, so str() replicates my complete log since start of program. Which</p>
  
  <ol>
  <li>produces unwanted output</li>
  <li>must result in an ever growing log-buffer (consuming all memory at a given point in time)</li>
  </ol>
  
  <p>This samal demo shows the problem:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  int main(int argc, char **argv)
  {
      stringstream ss;
  
    ss &lt;&lt; "1\n1";
    ss &lt;&lt; "22\n22";
    ss &lt;&lt; "333\n333";
    ss &lt;&lt; "4444\n4444";
    ss &lt;&lt; "55555\n55555";
    ss &lt;&lt; "666666\n666666";
    ss &lt;&lt; "7777777\n7777777";
  
    for (std::string line; std::getline(ss, line); ) {
      cout &lt;&lt; line &lt;&lt; ",";
    }
    cout &lt;&lt; "\n";
  
    cout &lt;&lt; ss.str() &lt;&lt; "\n";
  
      return 0;
  }
  </code></pre>
  
  <p>And the resulting output is:</p>
  
  <pre><code>1,122,22333,3334444,444455555,55555666666,6666667777777,7777777,
  1
  122
  22333
  3334444
  444455555
  55555666666
  6666667777777
  7777777
  Hit any key to continue...
  </code></pre>
  
  <p>So the first Line of output is what getline extracted, and afterwards std() still returns the complete string from the stream.</p>
  
  <p>Also I am a bit puzzled, why getline also extracts the last "7777777" as it is not followed by a linebreak.</p>
  
  <p>So my question is, how to extract lines, and only lines followed by a '\n' from a stringstream, extract in the sense of: what is read from the stringstream is no longer in the stringstream and also leaving incomplete lines untouched inside the stream - for later processing.</p>
  
  <p>PS: I can not use boost, its embedded stuff and boost is way to much here. </p>