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