所以我有一个hdf5文件,其中包含一个数据集:
DATASET "updateDateTime" {DATATYPE H5T_STRING{
STRSIZE 24;
STRPAD H5T_STR_NULLPAD;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE{ (5) / (5) }
DATA{
(0) : "2015-05-12\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(1) : "2015-05-13\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(2) : "2015-05-14\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(3) : "2015-05-15\000\000\000\000\000\000\000\000\000\000\000\000\000\000",
(4) : "2015-05-16\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
}我想使用C阅读这个数据集,但是我找不到合适的例子(我是HDF5新手)。具体来说,我不知道在阅读时使用哪个H5T_NATIVE_*。下面是我现在的代码:
hid_t time_ds = H5Dopen(grp, "updateDateTime", H5P_DEFAULT);
auto time_shape = get_dataset_shape(time_ds);
char** time_str = (char **)malloc(time_shape[0] * sizeof(char *)); // TODO: memeory allocation correct??
status = H5Dread(time_ds, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT,
time_str);
/*do my stuff*/
free(time_str);
status = H5Dclose(time_ds);发布于 2017-05-30 11:33:53
在深入研究了h5dump的源代码(该工具与hdf5包一起使用)之后,我终于让它开始工作了。我不能说这是一个很好的解决办法,但希望这能帮助其他遇到类似问题的人。
结果表明,本机类型可以由这个函数来推测。
hid_t h5tools_get_native_type(hid_t type)
{
hid_t p_type;
H5T_class_t type_class;
type_class = H5Tget_class(type);
if (type_class == H5T_BITFIELD)
p_type = H5Tcopy(type);
else
p_type = H5Tget_native_type(type, H5T_DIR_DEFAULT);
return(p_type);
}然后,按如下方式读取数据集:
type = H5Dget_type(dset);
native_type = h5tools_get_native_type(type);
auto shape = get_dataset_shape(dset);
n_element = std::accumulate(shape.begin(), shape.end(), 1ull, std::multiplies<size_t>());
type_size = std::max(H5Tget_size(type), H5Tget_size(native_type));
size_t alloc_size = n_element * type_size;
char * buf = BAT_NEW char[alloc_size];
status = H5Dread(dset, native_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf);
/*do my stuff*/
H5Tclose(native_type);
H5Tclose(type);
delete[] buf;发布于 2017-05-30 06:18:58
试一试
char* time_str = (char*) malloc(time_shape[0] * sizeof(char));
status = H5Dread(time_ds, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, &time_str);发布于 2018-01-07 15:25:35
或者,您可以使用C中的H5T_STRING读取数据集(数据类型为HDFql ),如下所示:
hdfql_execute("SELECT FROM updateDateTime");
hdfql_cursor_first(NULL);
printf("Dataset value is %s\n", hdfql_cursor_get_char(NULL));如果dataset存储了多个字符串(通过查看上面发布的h5dump结果,这似乎就是您的情况),您可以通过循环结果集来检索这些字符串:
hdfql_execute("SELECT FROM updateDateTime");
while(hdfql_cursor_next(NULL) == HDFQL_SUCCESS)
{
printf("Dataset value is %s\n", hdfql_cursor_get_char(NULL));
}https://stackoverflow.com/questions/44253668
复制相似问题