title: C++ read / write N exact bytes from file (even '00' bytes)
body: <p>My task is read file contents and place them into vector array of std::strings, then output from vector to another file. </p>
  
  <p>I managed to solve it for file containing only normal text, but failed with, lets say .bmp
  Here is .bmp file header:</p>
  
  <pre><code>BM)(     6   (   —  к        Р((
  </code></pre>
  
  <p>hex shows these are NULL bytes, not whitespaces</p>
  
  <pre><code>42 3d 06 29 28 00 00 00 00 00 36 00 00 00 ...
  </code></pre>
  
  <p>As a result , executing the following code</p>
  
  <pre><code>char *buffer = new char [50];
  
  ifstream ifs( "file.bmp" , std::ifstream::binary );
  ifs &gt;&gt; std::noskipws;
  
  ifs.read( buffer, 50 );
  
  std::string abc(buffer); //output it to other file next
  </code></pre>
  
  <p>i end up with buffer and abc strings equal to "BM)(6(—кР(( ..." with all nullbytes skipped.
  ifs.read() was great until now. So whats the most comfortable way to read/write N exact bytes from a file ?</p>
  
  <p>(I did a search but had difficulties with results)</p>