【问题标题】:HDF5 Cpp - retrieving the names of all groups in a fileHDF5 Cpp - 检索文件中所有组的名称
【发布时间】:2017-12-03 09:34:28
【问题描述】:

我尝试读取并保存文件中所有组的名称(在文件的根组中)。 所以我使用H5Literate 来遍历根组中的所有对象。

我的问题是,我可以打印组的名称,但我不知道如何将它们保存在数组中。

我的运算符函数如下所示:

extern "C" herr_t file_info(hid_t loc_id, int *mom, string *Moments, const char *name, const H5L_info_t *linfo,
void *opdata);
{
hid_t group;

group = H5Gopen2(loc_id, name, H5P_DEFAULT);

cout << "Name : " << name << endl;
Moments[mom]=name;
H5Gclose(group);
mom++;
return 0;
}

迭代如下所示:

Group *rootGr = new Group (file->openGroup("/"));
    hsize_t numMom = rootGr->getNumObjs();

    string Moments[numMom];
    int mom=0;

    herr_t idx = H5Literate(rootGr->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL);

cout 有效,但在尝试将名称保存在 Moments[mom] 时出现错误。

这里有什么问题?

编辑:错误是:

invalid conversion from 'herr_t (*)(hid_t, int*, std::string*, const char*, const H5L_info_t*, void*) {aka int (*)(int, int*, std::basic_string<char>*, const char*, const H5L_info_t*, void*)}' to 'H5L_iterate_t {aka int (*)(int, const char*, const H5L_info_t*, void*)}' [-fpermissive]    C/C++ Problem

Invalid arguments '
Candidates are:
int H5Literate(int, enum H5_index_t, enum {H5public.h:9387}, unsigned long long int *, int (*)(int, const char *, const {H5Lpublic.h:3236} *, void *), void *)
    Semantic Error

invalid types 'std::string* {aka std::basic_string<char>*}[int*]' for array subscript   C/C++ Problem

  initializing argument 5 of 'herr_t H5Literate(hid_t, H5_index_t, H5_iter_order_t, hsize_t*, H5L_iterate_t, void*)' [-fpermissive] C/C++ Problem

编辑 2:

我修好了:

我添加了一个全局

std::vector<string> GroupNames;

那么我的算子函数如下所示:

herr_t
file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
{
hid_t group;

group = H5Gopen2(loc_id, name, H5P_DEFAULT);

GroupNames.push_back(name);
cout << "Name : " << name << endl;
H5Gclose(group);
return 0;
}

【问题讨论】:

  • 你得到的错误是什么?
  • 我已将其添加到问题中。查看编辑(:
  • 我解决了这个问题 - 谢谢
  • 好的,很好:)。很高兴它成功了

标签: c++ hdf5


【解决方案1】:

即使全局变量“有效”,您也应该通过 void 指针传递数据。

herr_t
file_info(hid_t loc_id, const char *name, const H5L_info_t *linfo, void *opdata)
    {
    hid_t group;
    auto group_names=reinterpret_cast< std::vector<std::string>* >(opdata);
    group = H5Gopen2(loc_id, name, H5P_DEFAULT);
    //do stuff with group object, if needed
    group_names->push_back(name);
    cout << "Name : " << name << endl;
    H5Gclose(group);
    return 0;
    }

新的调用变成

 std::vector<std::string> group_names;
 herr_t idx = H5Literate(rootGr->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, &group_names);

额外练习:如果 group_names->push_back(name) throws 会发生什么?

【讨论】:

  • 我不明白。如果您所做的只是推回参数中已经存在的名称,为什么还需要 H5Gopen2()H5Gclose()
  • 您不需要打开群组。在我的代码中,我需要列出组的内容。要点是你应该传递一个指向额外数据的指针。
  • 好的。知道了。用注释修复了您的代码。希望没关系:)
  • 没关系。但正如答案中所说,组句柄应该有 RAII 管理,并且整个事情必须有一个 try 块,因为库不期望异常离开回调。
猜你喜欢
  • 2021-11-14
  • 2017-04-01
  • 2015-07-16
  • 1970-01-01
  • 2019-04-04
  • 2015-06-03
  • 2012-08-01
  • 2011-04-11
  • 1970-01-01
相关资源
最近更新 更多