hdf5:勾选创建组
hdf5: Check to create group
如果群组尚不存在,我想自动创建一个群组。但是,我没有成功检查该组是否存在。
失败尝试 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)
总是 returns 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
您的 尝试 1 失败,因为 H5::Group
不是属性,所以
if ( !file.attrExists(group_name.c_str()) )
并不像您想象的那样。 AFAIK,没有比尝试打开组更简单的方法来检查组是否存在。这导致我们
你的 尝试 2 实际上 没有失败 ,但有效(你为什么不这么认为?)不幸的是,HDF5 吐出很多错误 blurb(除了抛出异常),但你可以抑制它
int main(void)
{
H5::Exception::dontPrint(); // suppress error messages
H5::H5File file("test.h5",H5F_ACC_TRUNC);
std::string group_name = "/test";
try {
H5::Group group = file.openGroup (group_name.c_str());
std::cerr<<" TEST: opened group\n"; // for debugging
} catch (...) {
std::cerr<<" TEST: caught something\n"; // for debugging
H5::Group group = file.createGroup(group_name.c_str());
std::cerr<<" TEST: created group\n"; // for debugging
}
H5::Group group = file.openGroup (group_name.c_str()); // for debugging
std::cerr<<" TEST: opened group\n"; // for debugging
}
生成
TEST: caught something
TEST: created group
TEST: opened group
如果群组尚不存在,我想自动创建一个群组。但是,我没有成功检查该组是否存在。
失败尝试 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)
总是 returns 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
您的 尝试 1 失败,因为 H5::Group
不是属性,所以
if ( !file.attrExists(group_name.c_str()) )
并不像您想象的那样。 AFAIK,没有比尝试打开组更简单的方法来检查组是否存在。这导致我们
你的 尝试 2 实际上 没有失败 ,但有效(你为什么不这么认为?)不幸的是,HDF5 吐出很多错误 blurb(除了抛出异常),但你可以抑制它
int main(void)
{
H5::Exception::dontPrint(); // suppress error messages
H5::H5File file("test.h5",H5F_ACC_TRUNC);
std::string group_name = "/test";
try {
H5::Group group = file.openGroup (group_name.c_str());
std::cerr<<" TEST: opened group\n"; // for debugging
} catch (...) {
std::cerr<<" TEST: caught something\n"; // for debugging
H5::Group group = file.createGroup(group_name.c_str());
std::cerr<<" TEST: created group\n"; // for debugging
}
H5::Group group = file.openGroup (group_name.c_str()); // for debugging
std::cerr<<" TEST: opened group\n"; // for debugging
}
生成
TEST: caught something
TEST: created group
TEST: opened group