Reading a string from hdf5 in C ++

I read in datasets from an H5 file in Microsoft Visual C ++ 2008. Everything works fine for data like int and double, but I run into problems when I run into strings. In the H5 file, I have 18 lines of each fixed length of 24. My code is as follows:

StrType strdatatype(PredType::C_S1, 24); char *buffer1[18]; DataSet datasetCurveNames = riskFactorsH5.openDataSet("/gstrCurveNames"); datasetCurveNames.read(&buffer1, strdatatype); 

In execution buffer 1, bad pointers are filled. As an alternative, I tried using H5T_VARIABLE to control variable-length strings with a modification:

  StrType strdatatype(PredType::C_S1, H5T_VARIABLE); 

It also fails. If someone can shed light on this issue, it will be very appreciated.

Cheers, Lucas

+2
source share
3 answers

The HDF5 C ++ API is poorly documented. This is how I read rows from a dataset. I just figured it out using the IDE to complete the code:

 using namespace H5; std::string field_name("name of the field"); StrType datatype(0, H5T_VARIABLE); DataSpace dataspace(H5S_SCALAR); DataSet datset = group.openDataSet(field_name); std::string field_value; datset.read(field_value, datatype, dataspace); 
+6
source

You need to allocate memory for full lines, the library will not do this for you. You must replace

 char *buffer1[18]; 

by

 char buffer1[18][24]; 

and

 datasetCurveNames.read(&buffer1, strdatatype); 

it should be

 datasetCurveNames.read(buffer1, strdatatype); 

(no & )

0
source
  auto dataset = file.openDataSet(kDatasetName); auto dataspace = dataset.getSpace(); hsize_t dims_out[2]; auto ndims = dataspace.getSimpleExtentDims(dims_out, nullptr); assert(ndims == 2); auto n = dims_out[0] * dims_out[1]; auto data_type = dataset.getDataType(); auto type_class = data_type.getClass(); auto data_size = data_type.getSize(); void* out = new char[n * data_size](); dataset.read(out, data_type); if (type_class == H5T_INTEGER) { } else if (type_class == H5T_STRING) { std::string* strs = new std::string[n]; for (auto i = 0u; i < n; ++i) { auto len = data_size; auto c_str = out + data_size * i; for (auto p = c_str + len - 1; p != c_str && !*p; --p) --len; strs[i].assign(c_str, len); } } free(out); 

Checkout https://github.com/opentradesolutions/openalpha/blob/hdf5/src/openalpha/data.cc for a complete example

0
source

Source: https://habr.com/ru/post/1201628/


All Articles