HDF5 C ++读取属性的内容

sma*_*ica 1 c++ attributes hdf5

我尝试读取属性的内容。我使用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;

}
Run Code Online (Sandbox Code Playgroud)

应该写的test是:

ATTRIBUTE "lat" {
                  DATATYPE  H5T_IEEE_F64LE
                  DATASPACE  SCALAR
                  DATA {
                  (0): 48.3515
                  }
               }
Run Code Online (Sandbox Code Playgroud)

但是我得到的是:

lÐÞþ,H@
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我做错了什么吗?

坦克很多!

Tim*_*own 5

我同意@Mathias。但是,您还需要为其提供以下地址test

double test = 0.0;

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

cout << test << endl;
Run Code Online (Sandbox Code Playgroud)

执行程序时,您将获得:

$ h5c++ -o test1 test1.cpp && ./test1
48.3515
$
Run Code Online (Sandbox Code Playgroud)

我不是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;

}
Run Code Online (Sandbox Code Playgroud)

屈服:

$ h5c++ -o test test.cpp &&./test
48.3515
$
Run Code Online (Sandbox Code Playgroud)