【问题标题】:HDF5 C++ read the contents of an attributeHDF5 C++ 读取属性的内容
【发布时间】:2014-05-15 13:42:47
【问题描述】:

我尝试读取属性的内容。我使用 C++ 和 HDF5 API。 我的脚本如下所示:

#include <iostream>
#include <string>
#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif // H5_NO_STD
#endif
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

const H5std_string FILE_NAME ("file.h5");
const H5std_string GROUP_NAME_what ("here/where");

int main (void)
{


    H5File file( FILE_NAME, H5F_ACC_RDONLY );

    /*
     * open Group and Attribute
     */
    Group what= file.openGroup( GROUP_NAME_what );
    Attribute attr = what.openAttribute("lat");


    H5std_string test;

    DataType type = attr.getDataType();
    attr.read(type,test);

    cout << test << endl;

    return 0;

}

test应该写的是:

ATTRIBUTE "lat" {
                  DATATYPE  H5T_IEEE_F64LE
                  DATASPACE  SCALAR
                  DATA {
                  (0): 48.3515
                  }
               }

但我得到的是:

lÐÞþ,H@

谁能告诉我我做错了什么?

坦克很多!

【问题讨论】:

    标签: c++ attributes hdf5


    【解决方案1】:

    您尝试将浮点数 H5T_IEEE_F64LE 写入字符串 (H5std_string),但这不起作用。尝试改用float test

    【讨论】:

      【解决方案2】:

      我同意@Mathias。但是你还需要给它test的地址:

      double test = 0.0;
      
      DataType type = attr.getDataType();
      attr.read(type,&test);
      
      cout << test << endl;
      

      当你执行你的程序时,你会得到:

      $ h5c++ -o test1 test1.cpp && ./test1
      48.3515
      $
      

      我不是 C++ 程序员,但您似乎没有做整个 OO'nes,因为以下内容不是更有意义吗?

      int main (void)
      {
      
              double test = 0.0;
      
              H5File    *file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
              Group     *what = new Group (file->openGroup( GROUP_NAME_what ));
              Attribute *attr = new Attribute(what->openAttribute("lat"));
              DataType  *type = new DataType(attr->getDataType());
      
              attr->read(*type, &test);
              cout << test << endl;
      
              delete type;
              delete attr;
              delete what;
              delete file;
      
              return 0;
      
      }
      

      产量:

      $ h5c++ -o test test.cpp &&./test
      48.3515
      $
      

      【讨论】:

      • 谢谢你们!我现在可以阅读这些属性。 @Timothy:我也不是(C++)程序员(;你能解释一下我“没有做整个 OO'nes”的意思吗?
      • 就像创建命名空间、类和对象一样。 Wikipedia 有一个很好的部分。在上面的代码中,使用new H5File创建文件对象vs调用函数H5Fcreate()。希望对您有所帮助。
      • 是的,帮了很多忙!那么,我当时做了什么,即编写“Group what= file.openGroup( GROUP_NAME_what );”?尝试使用:“openGroup”作为函数而不是类的方法?
      猜你喜欢
      • 2015-09-29
      • 2014-05-05
      • 2017-09-29
      • 2014-05-11
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多