使用HDF5 C++ api在数据集上设置属性

Mar*_*arc 14 c++ hdf5

我在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); 
Run Code Online (Sandbox Code Playgroud)

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

Sam*_*ell 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" );
Run Code Online (Sandbox Code Playgroud)

添加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 );
Run Code Online (Sandbox Code Playgroud)

  • 字符串类型应该是`StrType strtype(PredType :: C_S1,H5T_VARIABLE);` (5认同)
  • 我得到了一个带有`att.write(str_type,"myString")的段错误;`它用`att.write(str_type,std :: string("myString"))`; (3认同)