How can attributes of an existing object be modified?


once again I have to ask for advice here, but I am confident that your excellent support can help me with this. For my usecase, I have an initialization phase in which I create the structure (groups, attributes) for the file and give some attributes a default value. Later on, I am trying to come back to the attributes and modify them. My current attempt is found below:

#include <iostream>
#include <cstdint>
#include <string>
#include "H5Cpp.h"

const H5std_string FILE_NAME( "test.h5" );

int main (void)
   * Create a new file. If file exists its contents will be overwritten.
  H5::H5File file(FILE_NAME, H5F_ACC_TRUNC);

   * Create Groups and Attributes
  H5::Group g_data(file.createGroup( "/Data" ));
  H5::DataSpace dspace = H5::DataSpace(H5S_SCALAR);
  H5::Attribute version = g_data.createAttribute("version", H5::PredType::NATIVE_INT, dspace);
  int curVersion = {1};
  version.write(H5::PredType::NATIVE_INT, &curVersion);

  // WORK HERE ...

  H5::Attribute new_version = file.openAttribute("/Data/version");
  int newVersion = {2};
  new_version.write(H5::PredType::NATIVE_INT, &newVersion);


  return 0;

I do, however, get an error when executing this:

HDF5-DIAG: Error detected in HDF5 (1.10.0-patch1) thread 140574792701760:
  #000: ../../../src/H5A.c line 438 in H5Aopen(): unable to load attribute info from object header for attribute: '/Data/version'
    major: Attribute
    minor: Unable to initialize object
  #001: ../../../src/H5Oattribute.c line 530 in H5O_attr_open_by_name(): can't locate attribute: '/Data/version'
    major: Attribute
    minor: Object not found
terminate called after throwing an instance of 'H5::AttributeIException'

It is probably due to the fact that I cannot use absolute paths from the file root to access attributes. The group objects have already been closed at the time of the modification, yet I want to access their attributes.

I was unable to find an example on this topic, so I would be grateful for some pointers here.

Best regards, S.

As you’ve suspected, the namespaces for HDF5 path names and HDF5 attributes are disjoint.
/ is a legitimate character in an attribute name, but not in a link name. In the C-API, you’d do something like
H5Aopen_by_name(file_id, "/Data", "version", H5P_DEFAULT, H5P_DEFAULT). I’m not sure if there is a one-shot variant in the C++ API.