Attribute with compound data, while writing data to individual fields erasing data from other fields (Same is working for dataset, but not working for attribute). Following is the sample code.
Note:I know writing all fields at a time works, but I want to write individual fields
Blockquote
#define REAL_FIELD_NAME “Real”
#define IMAGINARY_FIELD_NAME “Imag”
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, 0, H5::PredType::NATIVE_DOUBLE);
_comp_dt.insertMember(IMAGINARY_FIELD_NAME, sizeof(double), 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*/
typedef struct {
double real = 8;
double imag = 9;
} compl;
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;
}
> Blockquote