Without exactly knowing what you are trying to accomplish, I believe what you are looking for is a self-defined datatype using the HDF5 compound datatype H5::CompType, which is usually used to save simple structs. Taken from the HDF5 C++ compound example page, the struct typedef struct s1_t { int a; float b; double c; } s1_t;具有关联的化合物数据类型:has the associated compound datatype: CompType mtype1( sizeof(s1_t) ); mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT); mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE); mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);然后,将复合数据类型与本机数据类型相同地对待,也可以将其另存为属性.Compound datatyped are then treated the same way as native datatypes and may also be saved as attributes. 修改您在上面的代码中犯的错误是,当您实际上不想保存数组时,将数据类型定义为另存为H5 :: ArrayType.您真正想要的是保存在高维数据空间中的简单数据类型(例如PredType :: NATIVE_DOUBLE).The error you made in your code above was to define your datatype to be saved as an H5::ArrayType when you didn't actually want to save an Array. What you really want is a simple datatype (such as PredType::NATIVE_DOUBLE) saved in a higher dimensional dataspace.#include "H5Cpp.h"#ifndef H5_NO_NAMESPACE using namespace H5;#ifndef H5_NO_STD using std::cout; using std::endl;#endif // H5_NO_STD#endifconst H5std_string FILE_NAME("save.h5");const H5std_string ATT_NAME("Attribute");int main(){ const hsize_t dims=5; int ndims=1; DataType dtype=PredType::NATIVE_DOUBLE; H5File h5file(FILE_NAME, H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT); DataSpace* dspace = new DataSpace(ndims,&dims); Attribute att=h5file.createAttribute(ATT_NAME,dtype,*dspace); delete dspace; double attvalue[dims]; for(auto i=0;i<dims;++i) attvalue[i]=i; att.write(dtype,attvalue); h5file.close(); return 0;}这应该重现上面的"createdUsingHDFVIEW"属性(数据类型除外).我没有HDFView,因此无法检查.起初我没有想到这一点,因为我倾向于将H5 :: DataSpace视为一种数组类型(实际上是数组).This should reproduce the "createdUsingHDFVIEW" attribute above (except for the datatype). I can't check to make sure as I dont have HDFView. This didn't occur to me at first as I tend to think of H5::DataSpace as a type of array (which it actually is). 这篇关于如何使用C ++ API在HDF5文件中创建多值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 09:51