title: Writing binary data to fstream in c++
body: <h2>Question</h2>
  
  <p>I have a few structures I want to write to a binary file. They consist of integers from cstdint, for example <code>uint64_t</code>. Is there a way to write those to a binary file that doesn not involve me manually splitting them into arrays of <code>char</code> and using the <code>fstream.write()</code> functions?</p>
  
  <h2>What I've tried</h2>
  
  <p>My naive idea was that c++ would figure out that I have a file in binary mode and <code>&lt;&lt;</code> would write the integers to that binary file. So I tried this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdint&gt;
  
  using namespace std;
  
  int main() {
    fstream file;
    uint64_t myuint = 0xFFFF;
    file.open("test.bin", ios::app | ios::binary);
    file &lt;&lt; myuint;
    file.close();
    return 0;
  }
  </code></pre>
  
  <p>However, this wrote the string "65535" to the file.  </p>
  
  <p>Can I somehow tell the fstream to switch to binary mode, like how I can change the display format with <code>&lt;&lt; std::hex</code>?  </p>
  
  <p>Failing all that above I'd need a function that turns arbitrary cstdint types into char arrays.  </p>
  
  <p>I'm not really concerned about endianness, as I'd use the same program to also read those (in a next step), so it would cancel out.</p>