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.

matohak
  • 535
  • 4
  • 19
  • Check [Read a 3d Array from File](https://stackoverflow.com/q/17618997/10824407). – Olvin Roght Dec 09 '22 at 18:39
  • Does this answer your question? [Reading and writing binary file](https://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) – jjramsey Dec 09 '22 at 18:40
  • [Read a 3d Array from File](https://stackoverflow.com/questions/17618997/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](https://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) didn't mention anything from python. – matohak Dec 09 '22 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... – matohak Dec 09 '22 at 18:49

0 Answers0