Question

I try to read the contents of an Attribute. I use C++ and the HDF5 API. My script looks like this:

#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;

}

what should be written in test is:

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

but what I get is:

lÐÞþ,H@

Can someone tell my what I made wrong?

Tanks a lot!!

Was it helpful?

Solution

I agree with @Mathias. However you also need to give it the address of test:

double test = 0.0;

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

cout << test << endl;

When you execute your program you'll get:

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

I'm not a C++ programmer however it seems like your not doing the whole OO'nes, as in wouldn't the following make more sense?

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;

}

Yielding:

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

OTHER TIPS

You try to write a float H5T_IEEE_F64LE to a string (H5std_string) and that is not working. Try using float test instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top