HDF5(使用C++ API):写入属性复合数据的各个字段擦除其他字段

HDF5 (using C++ API): Writing individual fields of Attribute compound data erasing other fields

在写入属性结果的复合数据类型的各个字段时,其他字段数据被删除。有什么方法可以一次将单个字段写入属性复合数据?

注1:为复合数据类型的单个字段写入数据适用于数据集

注2:我知道写所有数据字段都行,但我们需要写个别字段

注 3:读取复合数据类型的各个字段适用于属性和数据集

 #define REAL_FIELD_NAME "Real"
#define IMAGINARY_FIELD_NAME "Imag"
typedef  struct {
    double real = 8;
    double imag = 9;
} compl;
int main()
{
    //Create a file
    std::string _file_name("test.h5");
    H5::H5File _file(_file_name, H5F_ACC_TRUNC);

    //Create dataspace
    hsize_t _dims = 1;
    H5::DataSpace dataspace(1, &_dims);

    //Create compound data type with two fields, 1. Real, 2. Imag
    H5::CompType _comp_dt((2 * sizeof(double)));
    _comp_dt.insertMember(REAL_FIELD_NAME, HOFFSET(compl, real), H5::PredType::NATIVE_DOUBLE);
    _comp_dt.insertMember(IMAGINARY_FIELD_NAME, HOFFSET(compl, imag), H5::PredType::NATIVE_DOUBLE);

    //Create Dataset with compound data type
    H5::DataSet _dset(_file.createDataSet("/test_dataset", _comp_dt, dataspace));
    //Create Attribute with compound data type
    H5::Attribute _attr(_dset.createAttribute("/test_attr", _comp_dt, dataspace));


    double _real = 9;
    H5::CompType _comp_dt_real(sizeof(double));
    _comp_dt_real.insertMember(REAL_FIELD_NAME, 0, H5::PredType::NATIVE_DOUBLE);
    //Writing data to dtaset, only Real field
    _dset.write(&_real, _comp_dt_real);
    //Writing data to attribute, only Real field
    _attr.write(_comp_dt_real, &_real);

    double _imag = 8;
    H5::CompType _comp_dt_imag(sizeof(double));
    _comp_dt_imag.insertMember(IMAGINARY_FIELD_NAME, 0, H5::PredType::NATIVE_DOUBLE);
    //Writing data to dtaset, only Imag field
    _dset.write(&_imag, _comp_dt_imag);
    //Writing data to Attribute, only Imag field, which clears Real field
    _attr.write(_comp_dt_imag, &_imag);

    /*following works*/
    compl _real_imag;

    H5::Attribute _attr_2(_dset.createAttribute("/test_attr_2", _comp_dt, dataspace));
    H5::CompType _comp_dt_cmpl(sizeof(compl));
    _comp_dt_cmpl.insertMember(REAL_FIELD_NAME, HOFFSET(compl, real), H5::PredType::NATIVE_DOUBLE);
    _comp_dt_cmpl.insertMember(IMAGINARY_FIELD_NAME, HOFFSET(compl, imag), H5::PredType::NATIVE_DOUBLE);
    _attr_2.write(_comp_dt_cmpl, &_real_imag);

    return 0;
}

结果:

我从 HDF5 论坛得到了回复。不允许写入部分属性值。

仅供参考:https://forum.hdfgroup.org/t/writing-individual-fields-of-compound-data-of-an-attribute-erasing-remaining-fields/7612