2011-05-13 254 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