title: Writing binary in c++ and read in python
body: <p>I would like to store a series of numbers to a binary file using c++, to open later using python. Currently I have the following script in c++ named *writer.cpp:</p>
  
  <pre><code>#include &lt;fstream&gt;
  
  int main()
  {
    std::ofstream outFile;
    outFile.open("binaryFile.bin", std::ios::binary);
    int number = 0;
    for (int i=0; i&lt;10; i++){
      number += i;
      outFile.write(reinterpret_cast&lt;const char*&gt;(&amp;number), sizeof(int));
    }
    outFile.close();
  
    return 0;
  }
  </code></pre>
  
  <p>Which when compiling as </p>
  
  <pre><code>g++ -o writerTest.x writer.cpp
  </code></pre>
  
  <p>and run as </p>
  
  <pre><code>./writerTest.x
  </code></pre>
  
  <p>produce a binary file named "binaryFile.bin".</p>
  
  <p>I then try to read this using python with the following script named <em>reader.py</em>:</p>
  
  <pre><code>import numpy as np
  from sys import argv
  
  path = str(argv[1])
  x = np.fromfile(path)
  print x, type(x)
  </code></pre>
  
  <p>Running this as 
      python reader.py binaryFile.bin
  produce the following result</p>
  
  <pre><code>[  2.12199579e-314   1.27319747e-313   3.18299369e-313   5.94158822e-313
  9.54898106e-313] &lt;type 'numpy.ndarray'&gt;
  </code></pre>
  
  <p>Which obviously is not what I was hoping for. What am I doing wrong, and how should this be done properly?</p>