title: Write vector of unsigned char to binary file c++
body: <p>I am reading binary file cmd.exe into unsigned chars array. Total bytes read into bytes_read are 153. I converted it to base64 string and then decode this string back (code from 2nd answer <a href="https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c">base64 decode snippet in c++</a>) into vector&lt;'BYTE>. Here BYTE is unsigned char.
  decodedData.size() is also 153. But when I write this vector to file in binary mode to get my cmd.exe file again I get only 1 KB file. What thing I missed? </p>
  
  <pre><code>// Reading size of file
      FILE * file = fopen("cmd.exe", "r+");
      if (file == NULL) return 1;
      fseek(file, 0, SEEK_END);
      long int size = ftell(file);
      fclose(file);
  // Reading data to array of unsigned chars
      file = fopen("cmd.exe", "r+");
      unsigned char * myData = (unsigned char *)malloc(size);
      int bytes_read = fread(myData, sizeof(unsigned char), size, file);
      fclose(file);
  
      std::string encodedData = base64_encode(&amp;myData[0], bytes_read);
      std::vector&lt;BYTE&gt; decodedData = base64_decode(encodedData);
  
  ////write data to file
      ofstream outfile("cmd.exe", ios::out | ios::binary);
      outfile.write((const char *)decodedData.data(), decodedData.size());
  </code></pre>
  
  <p>Update:
  Thanks @chux for suggesting "r+" --> "rb+" Problem resolved.</p>