Georgia S
Georgia S

Reputation: 622

C++ Code Creating Empty HDF5 File Instead of Dataset

I have the following code in my class to create a HDF5 file with a matrix of zeros. However, it creates an empty hdf5 file with no datasets at all. How do I write the dataset to the file?

#include <H5Cpp.h>
#include <Eigen/Dense>

H5::H5File file("test_save.hdf5", H5F_ACC_TRUNC);
hsize_t dims[2] {10, 5};


std::cout << dims[0] << " " << dims[1] << std::endl;
std::cout << data_set_name << std::endl;
H5::DataSpace dataspace(2, dims);

auto dataset = file.createDataSet("test_dataset", H5::PredType::NATIVE_DOUBLE, dataspace);
Eigen::MatrixXd temp(10, 5);
temp.setZero();
dataset.write(temp.data(), H5::PredType::NATIVE_DOUBLE);

dataset.close();
file.close();

Upvotes: 0

Views: 539

Answers (1)

SOG
SOG

Reputation: 912

Not sure how this is done with the H5Cpp library, but with HDFql it can be solved as follows (please note that the data written will be zeroes since you call temp.setZero();):

#include <HDFql.hpp>
#include <Eigen/Dense>

HDFql::execute("CREATE TRUNCATE AND USE FILE test_save.hdf5");

HDFql::execute("CREATE DATASET test_dataset AS DOUBLE(10, 5)")

Eigen::MatrixXd temp(10, 5);
temp.setZero();

sprintf(script, "INSERT INTO test_dataset VALUES FROM MEMORY %d", HDFql::variableTransientRegister(temp.data()));

HDFql::execute(script);

Upvotes: 0

Related Questions