14

我在 HDF5 1.8.7 中使用 HDF5 C++ API,并希望使用 H5::Attribute 实例在 H5::DataSet 实例中设置几个标量属性,但找不到任何示例。使用 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); 

由于某些奇怪的原因,C++ API 中的 H5::Attribute 和 H5::DataSet 类似乎缺少必要的方法。如果有人能提出使用 C++ API 的具体示例,我将不胜感激。

4

1 回答 1

18

如果你有一个数据集对象 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 );
于 2011-06-30T15:24:50.663 回答