2

I'm debugging some volumetric data in c++ and would like to visualise it.

Say I have a double array in c++, how can I write it as a binary file and read it from numpy as a 3D array? I'd like to stick to the standard c++ library.

double myArr[1000];
// ...Set values of myArr

std::ofstream outFile;
std::open("myfile.bin");
for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
            outFile << myArr[k + j*10 + i*100];
        }
        outFile << "some way to declare the next slice";
    }
    outFile << "some way to declare the next slice";
}
outFile.close();

In python I would like to read this array like this so it would appear as a 3D array:

numpy.fromfile("myfile.bin", dtype=np.float)

I read this Writing binary in c++ and read in python which works for 1d int but not sure how to create 3D double. ie. what do I need to write to the array to tell it to shift by 1 along each dimension? I don't want to use numpy to reshape the array as the dimension output by c++ can vary.

4
  • Check Read a 3d Array from File. Commented Dec 9, 2022 at 18:39
  • Does this answer your question? Reading and writing binary file Commented Dec 9, 2022 at 18:40
  • Read a 3d Array from File didn't mention anything on c++? I would like the array to be a binary file not text. And Reading and writing binary file didn't mention anything from python. Commented Dec 9, 2022 at 18:43
  • The challenge I'm having is reading/writing binary files across different languages. I know how to read/write in each python and c++. So the links above aren't helping... Commented Dec 9, 2022 at 18:49

0

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.