title: Extracting lines from continuous flow of strings
body: <p>I want to extract lines from a continuous flow of string data. But the data is not well-formatted, and I want to discard the extracted lines so that the memory doesn't go out of bound.</p>
  
  <p>For example, I have serial port outputs such as this:</p>
  
  <p>Batch 1</p>
  
  <pre><code>POS 30 10 20\n
  POS 30 10 21\n
  POS 30 1
  </code></pre>
  
  <p>Batch 2</p>
  
  <pre><code>        0 22\n
  POS 30 10 23\n
  PO
  </code></pre>
  
  <p>Is there a way to efficiently save the data and extract lines from it?</p>
  
  <p>This is my code:</p>
  
  <pre><code>stringstream ss;
  while (SP-&gt;IsConnected())
  {
      string result = SP-&gt;ReadData();
      ss &lt;&lt; result;
      string oneline;
      while(getline(ss, oneline))
      {
          cout &lt;&lt; oneline &lt;&lt; endl;
          // or do something more interesting...
      }
  }
  </code></pre>
  
  <p>But my code doesn't work since getline() modifies the stringstream so that no more data can be inserted to it, and I want to keep the stringstream memory small. So I don't think I can use getline() and use something like 'popline()' instead, if it exists.</p>