我目前正在探索HDF5。我已经阅读了线程“Evaluating HDF5”中有趣的评论,我知道HDF5是存储数据的选择解决方案,但如何查询它呢?例如,假设我有一个包含一些标识符的大文件:有没有快速知道某个给定的标识符是否存在于文件中的方法?
我目前正在探索HDF5。我已经阅读了线程“Evaluating HDF5”中有趣的评论,我知道HDF5是存储数据的选择解决方案,但如何查询它呢?例如,假设我有一个包含一些标识符的大文件:有没有快速知道某个给定的标识符是否存在于文件中的方法?
MainDataset (sorted on identifier)
0: { A, "C", 2 }
1: { B, "B", 1 }
2: { C, "A", 3 }
StringIndex
0: { "A", Reference ("MainDataset", 2) }
1: { "B", Reference ("MainDataset", 1) }
2: { "C", Reference ("MainDataset", 0) }
IntIndex
0: { 1, Reference ("MainDataset", 1) }
1: { 2, Reference ("MainDataset", 0) }
2: { 3, Reference ("MainDataset", 2) }
H5Lexists是在HDF5 1.8.0中引入的:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-Exists
您还可以使用H5Literate迭代HDF5文件中的内容:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-Iterate
但您也可以通过尝试打开数据集来手动检查以前的版本。我们使用以下代码来处理任何版本的HDF5:
bool DoesDatasetExist(const std::string& rDatasetName)
{
#if H5_VERS_MAJOR>=1 && H5_VERS_MINOR>=8
// This is a nice method for testing existence, introduced in HDF5 1.8.0
htri_t dataset_status = H5Lexists(mFileId, rDatasetName.c_str(), H5P_DEFAULT);
return (dataset_status>0);
#else
bool result=false;
// This is not a nice way of doing it because the error stack produces a load of 'HDF failed' output.
// The "TRY" macros are a convenient way to temporarily turn the error stack off.
H5E_BEGIN_TRY
{
hid_t dataset_id = H5Dopen(mFileId, rDatasetName.c_str());
if (dataset_id>0)
{
H5Dclose(dataset_id);
result = true;
}
}
H5E_END_TRY;
return result;
#endif
}
你所说的 标识符 是什么意思?如果你指的是一个 属性,请查看这个教程。在 C 语言中:
status = H5Aread(attr_id, mem_type_id, buf);
status = H5Awrite(attr_id, mem_type_id, buf);