2014-04-03 75 views
1

我嘗試讀取屬性的內容。我使用C++和HDF5 API。 我的腳本是這樣的:HDF5 C++讀取屬性的內容

#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ÐÞþ,[email protected] 

誰能告訴我我做了什麼錯?

坦克很多!

回答

3

我同意@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 
$ 
+0

謝謝兩位!我現在可以閱讀這些屬性。 @Timothy:我也不是一個(C++)程序員(你能解釋一下我的意思嗎?「我不是在做整個OO'nes」嗎? – smaica

+1

就像在創建命名空間,類和對象一樣[Wikipedia]( http://en.wikipedia.org/wiki/Object-oriented_programming)有一個很好的章節,在上面的代碼中,使用'new H5File'創建一個文件對象,並調用函數'H5Fcreate()'。 –

+0

是的,幫了很大忙!所以,我做了什麼,即通過編寫「Group what = file.openGroup(GROUP_NAME_what)」;「?嘗試使用:」openGroup「作爲函數,而不是一類? – smaica

1

您嘗試將一個浮點數H5T_IEEE_F64LE寫入一個字符串(H5std_string)並且不起作用。嘗試使用float test代替。