title: Why is the beginning of my string disappearing?
body: <p>In the following C++ code, I realised that <code>gcount()</code> was returning a larger number than I wanted, because <code>getline()</code> consumes the final newline character but doesn't send it to the input stream.</p>
  
  <p>What I still don't understand is the program's output, though.  For input "Test\n", why do I get " est\n"?  How come my mistake affects the <em>first</em> character of the string rather than adding unwanted rubbish onto the end?  And how come the program's output is at odds with the way the string looks in the debugger ("Test\n", as I'd expect)?</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  {
      const int bufferSize = 1024;
      ifstream input( "test.txt", ios::in | ios::binary );
  
      vector&lt;char&gt; vecBuffer( bufferSize );
      input.getline( &amp;vecBuffer[0], bufferSize );
      string strResult( vecBuffer.begin(), vecBuffer.begin() + input.gcount() );
      cout &lt;&lt; strResult &lt;&lt; "\n";
  
      return 0;
  }
  </code></pre>