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;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
Hyejin
  • 29
  • 2
  • 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. – tadman Jul 09 '20 at 23:34
  • 1
    [See this answer](https://stackoverflow.com/questions/52068410/allocating-a-large-memory-block-in-c/52069368#52069368) if you insist on wanting to do things this way. – PaulMcKenzie Jul 09 '20 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. – Ted Lyngmo Jul 09 '20 at 23:54
  • 1
    Thank you for the replies. @tadman I've solve it the way you suggest. – Hyejin Jul 10 '20 at 00:25
  • Keep in mind this has a billion elements so hope you're prepared for the memory footprint. – tadman Jul 10 '20 at 01:35

0 Answers0