6

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?

filiphl
  • 921
  • 1
  • 8
  • 15
  • You cannot just cast and write into a file, you must serialize your data to a known format (an existing one, or one you create yourself). – spectras May 28 '16 at 20:14
  • You are writing integers and reading floats, that cannot work. (Binary) – Baum mit Augen May 28 '16 at 20:15
  • @spectras: In practice, assuming an optimizer that is not overly aggressive and a "reasonable" platform, OP's code is likely to work in most cases. In theory, you do have to bit-mask and reconstruct the thing into a series of characters, but meh. – Kevin May 28 '16 at 20:17
  • 1
    @Kevin: …and that the python interpreter was compiled with the same compiler as his code. In the end, that's a lot of assumptions for anything more than a small proof of concept. – spectras May 28 '16 at 20:33

3 Answers3

6

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.

aldanor
  • 3,371
  • 2
  • 26
  • 26
3

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)
slugonamission
  • 9,562
  • 1
  • 34
  • 41
1

Here is a sample code that shows how to write binary data in C and read it in Python:

In C:

#include <stdio.h>
#include <stdlib.h>

int main() {
   int data[5] = {10, 20, 30, 40, 50};
   FILE * fp;

   fp = fopen ("test.bin", "wb");
   fwrite(data, sizeof(int), 5, fp);
   fclose (fp);

   return 0;
}

In Python:

with open('test.bin', 'rb') as f:

b = f.read() data = list(struct.unpack('i' * (len(b) // 4), b)) print(data)

Note: This assumes that the binary data contains integers. You will need to modify the code accordingly for other data types. See more details with: https://letchatgpt.com/read-in-python-c-write-binary-file-from-vector/

Kim Marry
  • 55
  • 3
  • 1
    Good starting point for an answer, some small formatting fixes could be done for the Python code snippet. It would also be useful if you could explain what the C code does and why the Python example is able to read it – Tanishq dubey Jun 02 '23 at 14:36