title: Random characters in stringstream
body: <p>I'm writing a program that searches for files. This block of code writes a folder name to a stringstream, preceding it with a "./" so that it can be used to change the directory. It then changes the directory and informs the user of the change.</p>
  
  <pre><code>stringstream maindir;
  maindir &lt;&lt; "./" &lt;&lt; crntmainfile;
  maindir.str().copy(maindirectory, 260, 0);
  _chdir(maindirectory);
  std::cout &lt;&lt; maindirectory;
  std::cout &lt;&lt; "Main directory changed: " &lt;&lt; maindirectory &lt;&lt; "\n";
  </code></pre>
  
  <p>My problem is that <code>maindirectory</code> has a bunch of extra characters at the end. I'm assuming that this has to do with the fact that it's a 260 length array with maybe 20 characters in it and the extra characters are whatever happens to be occupying the unwritten memory. If this is my problem, how do I fix it?</p>
  
  <p>EDIT:
  I've determined that <code>crntmainfile</code> is null terminated, but the terminator is lost when it's written to <code>maindir</code>. What's causing this? Does <code>&lt;&lt;</code> simply not write null characters to stringstreams? How do I fix it?</p>
  
  <p>EDIT:
  I solved my problem by doing <code>maindir.put(0);</code> after <code>maindir &lt;&lt; "./" &lt;&lt; crntmainfile;</code> to manually null terminate the string.</p>