title: Print std::vector<char> without extraneous characters
body: <p>I'm building a crude replacement for gunzip using the Poco libraries.  Right now the design is to read a gzipped file (currently hardcoded to "data.gz") and print the output to stdout.  I'm very close, but having some trouble as it is printing some extra characters, as shown below.</p>
  
  <p>Setup:</p>
  
  <p>Ubuntu 19.10 with libpocofoundation62 (and all libpoco*62) installed via <code>apt install libpoco-dev</code></p>
  
  <p>My c++ code, modeled heavily off of <a href="https://github.com/pocoproject/poco/issues/1507" rel="nofollow noreferrer">https://github.com/pocoproject/poco/issues/1507</a></p>
  
  <pre><code>#define _GLIBCXX_USE_CXX11_ABI 0
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;Poco/InflatingStream.h&gt;
  #include &lt;Poco/String.h&gt;
  #include &lt;vector&gt;
  
  using std::cout;
  using std::endl;
  
  int main() {
  
          cout.sync_with_stdio(false);
          std::ifstream istr("data.gz", std::ios::binary); // In the future will take filename as argument
          Poco::InflatingInputStream inflating_stream(istr, Poco::InflatingStreamBuf::STREAM_GZIP);
          std::vector&lt;char&gt; buf(16); // In the future will be larger, like 1024
          while (true) {
                  inflating_stream.read(buf.data(), buf.size());
                  size_t gcount = inflating_stream.gcount();
  
                  if (!gcount &amp;&amp; inflating_stream.eof()) {
                          inflating_stream.reset();
                  }
  
                  // This way outputs all the correct data, but also some extraneous characters at the end
                  if (gcount) {
                          cout &lt;&lt; buf.data();
                  }
  
                  /* This way works, but is slower
                  if (gcount) {
                          for (auto i: buf) {
                                  std::cout &lt;&lt; i;
                          }
                  }
                  */
  
                  else {
                          break;
                  }
          }
  
          return 0;
  }
  </code></pre>
  
  <p>My data.gz</p>
  
  <pre><code>$ zcat data.gz 
  foo, bar
  baz, qux
  quux, quuz
  corge, grault
  garply, waldo
  fred, plugh
  xyzzy, thud
  </code></pre>
  
  <p>My compilation command:</p>
  
  <pre><code>g++ mygunzip.cpp -o /tmp/mygunzip -lPocoFoundation -lPocoUtil &amp;&amp; chmod +x /tmp/mygunzip
  </code></pre>
  
  <p>The result of running mygunzip:</p>
  
  <pre><code>$ /tmp/mygunzip
  foo, bar
  baz, qu��f�lUx
  quux, quuz
  cor��f�lUge, grault
  garpl��f�lUy, waldo
  fred, p��f�lUlugh
  xyzzy, thud��f�lU
  ugh
  xyzzy, thud��f�lU
  </code></pre>
  
  <p>So all of the correct data is being printed out, but there is extraneous data held in buf.data() after each read that is also being printed out.  What would be the most elegant way of handling the extraneous data?  I've included in the comments another way that works, and it prints out all of the correct data without the extraneous characters.  But it seems to be much slower, so I'm looking to improve on the other solution if possible.</p>