HDF5/C ++中存在测试组 [英] Test group existence in hdf5/c++

查看:73
本文介绍了HDF5/C ++中存在测试组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开一个现有的HDF5文件来附加数据;我要确保存在名为/A的组以用于后续访问.我正在寻找一种有条件的创建/A的简便方法(如果不存在,则创建并返回新组,或者返回现有组).一种方法是测试/A存在.我怎样才能有效地做到这一点?

I am opening an existing HDF5 file for appending data; I want to assure that group called /A exists for subsequent access. I am looking for an easy way to either create /A conditionally (create and return new group if not existing, or return the existing group). One way is to test for /A existence. How can I do it efficiently?

根据API文档,我可以执行以下操作:

According to the API docs, I can do something like this:

H5::H5File h5file(filename,H5F_ACC_RDWR);
H5::H5Group grp;
try{
   grp=h5file.openGroup("A");
} catch(H5::Exception& e){
   /* group does not exists, create it */
   grp=h5file.createGroup("A");
}

但是明显的丑陋来自于这样一个事实,即异常用于传达信息,这一点也不例外.

but the obvious ugliness comes from the fact that exception is used to communicate information which is not exceptional at all.

H5 :: CommonFG :: getObjinfo ,似乎将 H5Gget_objinfo 包裹为false(不存在)的C例程的返回值引发异常;同样的问题.

There is H5::CommonFG::getObjinfo, which seems to wrap H5Gget_objinfo in such way that false (nonexistent) return value of the C routine throws an exception; so again the same problem.

在这种情况下,使用C API是否干净,还是直接设计了一些功能来测试我忽略的C ++ API中的存在?

Is it clean to recourse to the C API in this case, or is there some function directly designed to test existence in the C++ API which I am overlooking?

推荐答案

正如Yossarian在其评论中以及在此答案中所建议的那样您可以使用HDF5 1.8.0+ C-API

As suggested by Yossarian in his comment and in this answer you can use HDF5 1.8.0+ C-API

bool pathExists(hid_t id, const std::string& path)
{
  return H5Lexists( id, path.c_str(), H5P_DEFAULT ) > 0;
}

H5::H5File h5file(filename,H5F_ACC_RDWR);
H5::H5Group grp;
if (pathExists(h5file.getId(), "A")) 
   grp=h5file.openGroup("A");
else
   grp=h5file.createGroup("A");

这篇关于HDF5/C ++中存在测试组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆