0

I want to read binary file and store data into 3D float array. I've seen some examples for 1D array, but don't know how to expand it to 3D.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    int N = 1024;
    // allocate 3d array
    float *** stmp = (float ***)malloc(N * sizeof(float**));
    for (int i = 0; i < N; i++) {
        stmp[i] = (float **)malloc(N * sizeof(float *));
        for (int j = 0; j < N; j++) {
            stmp[i][j] = (float *)malloc(N * sizeof(float));
        }
    }
    string path = "Velocity1_inertHIT.bin";
    ifstream infile;
    infile.open(path);


    return 0;
}
5
  • 1
    Since this is C++ use std::vector instead of this C-style malloc() mess. You probably don't want a 3D array, but an array of size N*N*N and then use emulation to access the elements. Commented Jul 9, 2020 at 23:34
  • 1
    See this answer if you insist on wanting to do things this way. Commented Jul 9, 2020 at 23:42
  • 2
    How are the binary floats stored? There's not just one way so that'd be my first prio to figure out. Commented Jul 9, 2020 at 23:54
  • 1
    Thank you for the replies. @tadman I've solve it the way you suggest. Commented Jul 10, 2020 at 0:25
  • Keep in mind this has a billion elements so hope you're prepared for the memory footprint. Commented Jul 10, 2020 at 1:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.