2011-05-13 260 views
14

我在HDF5 1.8.7中使用HDF5 C++ API,並且希望使用H5 :: Attribute實例在H5 :: DataSet實例中設置幾個標量屬性,但找不到任何例子。這是很添油加醋使用C API:使用HDF5設置數據集上的屬性C++ api

/* Value of the scalar attribute */ 
int point = 1;       

/* 
* Create scalar attribute for the dataset, my_dataset. 
*/ 
aid2 = H5Screate(H5S_SCALAR); 
attr2 = H5Acreate(my_dataset, "Integer attribute", H5T_NATIVE_INT, aid2,H5P_DEFAULT); 

/* 
* Write scalar attribute to my_dataset. 
*/ 
ret = H5Awrite(attr2, H5T_NATIVE_INT, &point); 

/* 
* Close attribute dataspace. 
*/ 
ret = H5Sclose(aid2); 

/* 
* Close attribute. 
*/ 
ret = H5Aclose(attr2); 

一些奇怪的原因,H5 ::屬性和C++ API中的H5 :: DataSet中的類似乎缺少必要的方法。如果任何人都能用C++ API想出一個具體的例子,我會非常感激。

回答

16

,如果你有一個數據集對象DS ...

添加一個字符串屬性...

StrType str_type(0, H5T_VARIABLE); 
DataSpace att_space(H5S_SCALAR); 
Attribute att = ds.createAttribute("myAttribute", str_type, att_space); 
att.write(str_type, "myString"); 

添加一個int屬性...

IntType int_type(PredType::STD_I32LE); 
DataSpace att_space(H5S_SCALAR); 
Attribute att = ds.createAttribute(" myAttribute", int_type, att_space); 
int data = 77; 
att.write(int_type, &data); 
+4

字符串類型應該真的是'StrType strtype(PredType :: C_S1,H5T_VARIABLE);' – Simon 2011-07-15 02:13:54

+1

createAttribute方法是爲H5 :: Object-s定義的,因此您可以使用相同的習慣用法將屬性附加到H5 :: Group -s,例如。 – 2013-10-26 09:11:18

+0

@Simon:[無論哪種方式工作得很好](http://www.hdfgroup.org/HDF5/doc/cpplus_RM/classH5_1_1StrType.html#a502e6a4895bf51314204179e3f093a7f) – 2013-12-10 06:12:58