【发布时间】:2017-11-06 10:00:24
【问题描述】:
如果组尚不存在,我想自动创建一个组。但是,我没有成功检查该组是否存在。
尝试 1 失败
#include <iostream>
#include <string>
#include "H5Cpp.h"
int main(void)
{
H5::H5File file("test.h5",H5F_ACC_TRUNC);
std::string group_name = "/test";
H5::Group group = file.createGroup(group_name.c_str());
if ( !file.attrExists(group_name.c_str()) )
H5::Group group = file.createGroup(group_name.c_str());
return 0;
}
编译
$ h5c++ -o test test.cpp
失败,因为file.attrExists(name) 总是返回false。供参考,错误发生在createGroup:
...
#006: H5L.c line 1733 in H5L_link_cb(): name already exists
major: Symbol table
minor: Object already exists
尝试 2 失败
#include <iostream>
#include <string>
#include "H5Cpp.h"
int main(void)
{
H5::H5File file("test.h5",H5F_ACC_TRUNC);
std::string group_name = "/test";
try { H5::Group group = file.openGroup (group_name.c_str()); }
catch (...) { H5::Group group = file.createGroup(group_name.c_str()); }
return 0;
}
编译
$ h5c++ -o test test.cpp
不知何故,catch 不成功,openGroup 失败时产生错误:
...
#005: H5Gloc.c line 385 in H5G_loc_find_cb(): object 'test' doesn't exist
major: Symbol table
minor: Object not found
【问题讨论】:
-
如果在“/test”(
group_name)中省略“/”会怎样? -
@Walter 感谢您的建议,但它不能解决两个提议的解决方案中的任何一个。您能解释一下为什么您认为删除第一个斜线会有所帮助吗?