0

I have one program written in C++ that outputs the data from several different types of arrays. For simplicity, I'm using ints and just writing them out one at a time to figure this out.

I need to be able to read the file in on Python, but clearly am missing something. I'm having trouble translating the concepts from C++ over to Python.

This is the C++ I have that's working - it writes out two numbers to a file and then reads that file back in (yes, I have to use the ostream.write() and istream.read() functions - that's how at the base level the library I'm using does it and I can't change it).

int main(int argc, char **argv) {
  std::ofstream fout;
  std::ifstream fin;

  int outval1 = 1234;
  int outval2 = 5678;

  fout.open("out.txt");
  fout.write(reinterpret_cast<const char*>(&outval1), sizeof(int));
  fout.write(reinterpret_cast<const char*>(&outval2), sizeof(int));
  fout.close();
  
  int inval;
  fin.open("out.txt");

  while (fin.read(reinterpret_cast<char*>(&inval), sizeof(int))) {
    std::cout << inval << std::endl;
  }

  fin.close();

  return 0;
}

This is what I have on the Python side, but I know it's not correct. I don't think I should need to read in as binary but that's the only way it's working so far

with open("out.txt", "rb") as f:
    while (byte := f.read(1)):
        print(byte)
4
  • If you want to write the file out as text, maybe use itoa() or even just fprintf? Then you can read it in as text on the Python side. Commented Nov 5, 2022 at 23:14
  • Those reinterpret_cast and write and read files look very suspect to me. You're lying and saying that &outval1 is a pointer to a const char and saying to write the first sizeof(int) many const chars from the "array" being pointed to. Which I suppose has the effect of writing out the binary representation of the int. But... really, why are you doing it that way to begin with? Commented Nov 5, 2022 at 23:15
  • You should open the files in binary mode in this code. It's a must to avoid corruption if you plan to run your program on Windows and just good practice to show intent on other platforms where it doesn't matter as much. Commented Nov 5, 2022 at 23:15
  • @NathanPierson that's just how the library is written. It's meant to take in a several different types of values from an array like structure and write them out. I suppose it was written like this to keep it flexible. I couldn't say Commented Nov 5, 2022 at 23:16

1 Answer 1

3

In the simple case you have provided, it is easy to write the Python code to read out 1234 and 5678 (assuming sizeof(int) is 4 bytes) by using int.from_bytes. And you should open the file in binary mode.

import sys

with open("out.txt", "rb") as f:
    while (byte := f.read(4)):
        print(int.from_bytes(byte, sys.byteorder))

To deal with floats, you may want to try struct.unpack:

import struct


byte = f.read(4)
print(struct.unpack("f", byte)[0])
Sign up to request clarification or add additional context in comments.

2 Comments

That ended up working for me. Only thing I'm wondering now is how to do the same thing for floats (output from C++)
@dave you may have a look at struct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.