Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing binary in c++ and read in python

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?

like image 816
filiphl Avatar asked Feb 07 '23 22:02

filiphl


2 Answers

You have to specify the type of the values you're going to be reading, numpy has no way to guess that as there is no metadata stored in the file itself. So in your case you have to do something like:

x = np.fromfile(path, dtype=int)

If you're doing things like this, it's highly recommended to use fixed-size integers instead of just int, e.g. in C++ you can use int32_t from <cstdint> and in Python you can specify int32 as dtype.

like image 74
aldanor Avatar answered Feb 09 '23 10:02

aldanor


fromfile assumes floating point numbers by default. If you want to change this behaviour, you need to pass in the type in the dtype named parameter.

As you're writing ints, this should work:

x = np.fromfile(path, dtype=int)
like image 33
slugonamission Avatar answered Feb 09 '23 12:02

slugonamission