title: Writing floats to a binary file in C++ | Equivalent of Java's DataOutputStream.writeFloat()
body: <p>I'm porting some code from Java to C++ and need to write floats to a binary file. In Java I use <code>DataOutputStream.writeFloat()</code>. How do I do it in C++?</p>
  
  <p>I tried this method but it doesn't work:</p>
  
  <pre><code>std::ofstream out;
  out.open(somePath, std::ios::out | std::ios::binary);
  
  float f = 0.5;
  out.write(reinterpret_cast&lt;const char*&gt;(&amp;f), sizeof(float));
  </code></pre>