从C ++中的hdf5读取字符串 [英] Reading a string from hdf5 in C++
问题描述
我在Microsoft Visual C ++ 2008中的H5文件中读取数据集。一切对于int类型和double类型的数据都很好,但是当我遇到字符串时遇到问题。在H5文件中,我有18个字符串,每个固定长度为24.我的代码如下:
I am reading in datasets from a H5 file in Microsoft Visual C++ 2008. Everything works fine for data of type int and double but I run into problems when I come across strings. In the H5 file I have 18 strings each of fixed length 24. My code is as follows;
StrType strdatatype(PredType::C_S1, 24);
char *buffer1[18];
DataSet datasetCurveNames = riskFactorsH5.openDataSet("/gstrCurveNames");
datasetCurveNames.read(&buffer1, strdatatype);
执行时,buffer1被错误指针填充。作为一个替代,我已经尝试使用H5T_VARIABLE管理变量长度字符串与修改:
On execution buffer1 is filled with bad pointers. As an alternative I have tried using H5T_VARIABLE to manage variable length strings with the modification:
StrType strdatatype(PredType::C_S1, H5T_VARIABLE);
这也失败。
干杯,
Lucas
Cheers, Lucas
推荐答案
HDF5 C ++ API非常缺乏文档。这是我从数据集的字符串中读取的方式。我只是在代码完成IDE的帮助下想出这一点:
The HDF5 C++ API is woefully under-documented. This is how I read in strings from a dataset. I only figured this out with the help of a code-completion IDE:
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);
这篇关于从C ++中的hdf5读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!