hello all,
i am tring this code on ubuntu 22 LTS:
#include "H5Cpp.h" // Include the HDF5 C++ API header
#include <iostream>
#include <vector>
using namespace H5;
int main() {
// File name and dataset name
const std::string FILE_NAME = "example.h5";
const std::string DATASET_NAME = "3D_Double_Array";
// Dimensions of the 3D array
const hsize_t DIM_X = 4; // First dimension
const hsize_t DIM_Y = 3; // Second dimension
const hsize_t DIM_Z = 2; // Third dimension
const hsize_t dims[3] = {DIM_X*2, DIM_Y*2, DIM_Z*2};
// Create a 3D array and fill it with values
std::vector<double> data(DIM_X * DIM_Y * DIM_Z);
for (hsize_t i = 0; i < DIM_X; ++i) {
for (hsize_t j = 0; j < DIM_Y; ++j) {
for (hsize_t k = 0; k < DIM_Z; ++k) {
data[i * DIM_Y * DIM_Z + j * DIM_Z + k] = static_cast<double>(i * 100 + j * 10 + k);
}
}
}
try {
// Create a new HDF5 file
H5File file(FILE_NAME, H5F_ACC_TRUNC);
// Create the dataspace for the dataset
DataSpace dataspace(3, dims);
// Create the dataset with datatype double
DataSet dataset = file.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE, dataspace);
// Write the data to the dataset
for (int i = 0; i< 8; i++) {
std::cout << "i " << i << ": " << data.data()[2] << " " << data.data()[3] << std::endl;
dataset.write(data.data(), PredType::NATIVE_DOUBLE);
}
std::cout << "3D array successfully written to " << FILE_NAME << std::endl;
} catch (FileIException &e) {
e.printErrorStack();
return -1;
} catch (DataSetIException &e) {
e.printErrorStack();
return -1;
} catch (DataSpaceIException &e) {
e.printErrorStack();
return -1;
}
return 0;
}
but the resulting h5 file does not contain correct values after the first call to data.data():
$ h5dump example.h5
HDF5 "example.h5" {
GROUP "/" {
DATASET "3D_Double_Array" {
DATATYPE H5T_IEEE_F64LE
DATASPACE SIMPLE { ( 8, 6, 4 ) / ( 8, 6, 4 ) }
DATA {
(0,0,0): 0, 1, 10, 11,
(0,1,0): 20, 21, 100, 101,
(0,2,0): 110, 111, 120, 121,
(0,3,0): 200, 201, 210, 211,
(0,4,0): 220, 221, 300, 301,
(0,5,0): 310, 311, 320, 321,
(1,0,0): 0, 2.42092e-322, 4.91634e-310, 4.94066e-324,
(1,1,0): 4.94066e-324, 4.94066e-324, 4.91634e-310, 1.63042e-322,
(1,2,0): 4.91634e-310, 4.91634e-310, 0, 5.85468e-321,
(1,3,0): 3.78577e-270, 4.91634e-310, 9.88131e-324, 9.38725e-323,
(1,4,0): 1.82918e-319, nan, 0, 4.94066e-324,
(1,5,0): 4.94066e-324, 0, 1.4822e-323, 2.02369e-320,
(2,0,0): 4.74303e-322, 4.74303e-322, 3.42966e+161, 1.1908e-307,
(2,1,0): 0, 0, 0, 0,
(2,2,0): 0, 0, 0, 0,
(2,3,0): 0, 0, 0, 0,
(2,4,0): 0, 0, 0, 0,
(2,5,0): 0, 0, 0, 0,
(3,0,0): 0, 0, 0, 0,
(3,1,0): 0, 0, 0, 0,
(3,2,0): 0, 0, 0, 0,
(3,3,0): 0, 0, 0, 0,
(3,4,0): 0, 0, 0, 0,
(3,5,0): 0, 0, 0, 0,
(4,0,0): 0, 0, 0, 0,
(4,1,0): 0, 0, 0, 0,
(4,2,0): 0, 0, 0, 0,
(4,3,0): 0, 0, 0, 0,
(4,4,0): 0, 0, 0, 0,
(4,5,0): 0, 0, 0, 0,
(5,0,0): 0, 0, 0, 0,
(5,1,0): 0, 0, 0, 0,
(5,2,0): 0, 0, 0, 0,
(5,3,0): 0, 0, 0, 0,
(5,4,0): 0, 0, 0, 0,
(5,5,0): 0, 0, 0, 0,
(6,0,0): 0, 0, 0, 0,
(6,1,0): 0, 0, 0, 0,
(6,2,0): 0, 0, 0, 0,
(6,3,0): 0, 0, 0, 0,
(6,4,0): 0, 0, 0, 0,
(6,5,0): 0, 0, 0, 0,
(7,0,0): 0, 0, 0, 0,
(7,1,0): 0, 0, 0, 0,
(7,2,0): 0, 0, 0, 1.13833e-320,
(7,3,0): 4.92301e-316, 0, 0, 1.63042e-322,
(7,4,0): 4.91634e-310, 4.91634e-310, 0, 1.63042e-322,
(7,5,0): 4.91634e-310, 4.91634e-310, 0, 1.13882e-320
}
}
}
}
however, the content of the data vector seems correct, since I display it before each write to the file.
Wher am i wrong?
Thanks in advance,
Gérard