title: Duplicating a file via fstream
body: <p>I'm trying to duplicate files by using an ifstream to read the file and ofstream to write back out, but having some problems. Here's what I have so far:</p>
  
  <pre><code>std::ifstream File(_str.c_str(), std::ios::binary);
  
  std::string strFile;
  strFile.assign( (std::istreambuf_iterator&lt;char&gt;(File) ),
          (std::istreambuf_iterator&lt;char&gt;() )   );
  
  File.close();
  
  return strFile;
  </code></pre>
  
  <p>What's happening is some values of the string are null, and the string is terminating right there. For example if it was</p>
  
  <pre><code>1 'h'
  2 'i'
  3 0
  4 'h'
  5 'i'
  </code></pre>
  
  <p>it would return simply "hi"</p>
  
  <p>I have tried various for loops and ways of getting around this, for example replacing the 0's with spaces, and replacing the 0's with "-NULL-" then on the user side replacing "-NULL-" back with 0 again, but I can't seem to add 0 to the string.</p>
  
  <p>Could anyone give me some tips for what I could do to get around this problem? What I'm trying to do is transfer a .png via UDP, which is why I'm chucking the file into a string</p>
  
  <p>Cheers!</p>