 <p>In this statement, you get number, use it as address in <em>memory</em>, which is most likely invalid.</p> <pre><code>file.write((char*)num, sizeof(num)); </code></pre> <p>If you want to write <code>num</code> in binary representation, you should get its address first:</p> <pre><code>file.write(reinterpret_cast&lt;char*&gt;(&amp;num), sizeof(num)); </code></pre> <p>Note the ampersand which is unary operator for getting addresses. I have also used <code>reinterpret_cast</code> which is C++ type conversion for such cases. C-style conversion may hide errors (but <code>reinterpret_cast</code> is also valid for <code>int</code>-><code>*</code> conversion).</p> <p>P.S. <code>void main()</code> is an invalid prototype of main. It should return <code>int</code> at least.</p>