How to modify attributes of an existing object with the HDF5 lib?

  attributes, c++, hdf5

I am trying to build an C++ application that utilizes the HDF5 library. For my usecase, I have an initialization phase in which I create the structure (groups, attributes) for the file and give them 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);


  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 groups objects have already been closed at the time of the modification, yet I want to access their attributes.

Does anybody know how this is done correctly?

Source: Windows Questions C++