title: Unable to write file in binary mode even with ios::binary
body: <p>I followed the tutorial on cplusplus.com for std::ofstream::binary and cannot seem to get the hex output that I need in binary mode.</p>
  
  <p>I currently need to do a project in cryptography and require the output to be in binary mode (and not writing the hex values into the contents of the file).</p>
  
  <p>Below is an excerpt from my code (with comments):</p>
  
  <pre><code>ofstream writeout(filename2.c_str(), ios::binary);
  
  //ctextvec is the vector that stores the ciphertext
  for (int i=0; i&lt;ctextvec.size(); i+=2)
  {
      ostringstream oss, oss2;
      //formatting of hex to string (adding missing '0')
      oss &lt;&lt; hex &lt;&lt; setw(16) &lt;&lt; setfill('0') &lt;&lt; ctextvec[i];
      oss2 &lt;&lt; hex &lt;&lt; setw(16) &lt;&lt; setfill('0') &lt;&lt; ctextvec[i+1];
  
      //Stores all ciphertext characters as a string
      storestr.append(oss.str());
      storestr.append(oss2.str());
  }
  
  //Define new array to store characters for writing
  char writearr[storestr.size()];
  for(int i=0; i&lt;storestr.size(); i++) writearr[i]=storestr[i];
  
  //Perform write to file
  for(int i=0; i&lt;storestr.size(); i++)
      writeout.write((char*)&amp;writearr[i],sizeof(char));
  writeout.flush();
  writeout.close();
  </code></pre>
  
  <p>I tried a lot of methods and all seemed to dump the contents into a file rather than writing into binary mode. I supposedly need this command to work to show that a file is encrypted (at least).</p>
  
  <p>Below are the screenshots of the file that I got and what I should be getting.</p>
  
  <p>What I'm getting:
  <a href="https://i.stack.imgur.com/3PaJL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3PaJL.png" alt="Content dump"></a></p>
  
  <p>What I'm supposed to get:
  <a href="https://i.stack.imgur.com/NJFhZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NJFhZ.png" alt="Supposed output"></a></p>
  
  <p>Any help is much appreciated!</p>