Opening an HDF5 for Update


#1

I created an HDF5 using:

	hdf5_file = H5::H5File(h5_filepath, H5F_ACC_TRUNC);

and added Groups and Datasets. I then ran another application that opened the same file using H5F_ACC_TRUNC and added more Groups and Datasets. These Groups and Datasets are overwriting the ones created by the first application. How do I open an existing file to update it?


#2

TRUNC stands for to truncate, you already discovered what it does, would you mind trying H5F_ACC_RDWR and H5F_ACC_RDONLY as well?
best wishes: steve


#3

When I use H5F_ACC_RDWR, I get an exception because the file doesn’t exist yet. How do I check if a file already exists? I’m using C++ and I see there are many member functions for the H5::H5File class. Where is the documentation for C++? The HDF5 User’s Guide, HDF% Release 1.0 does not cover C++. I’m using H5Cpp.h include.

–Mike


#4

I can’t speak for the official C++ API, nevertheless from H5CPP examples I offer you this idom:

#include <h5cpp/all>
int main(){
   h5::fd_t fd;  // CAPI binary compatible descriptor, initialised to `H5I_UNINIT`
   try {  // you could optionally use C++ calls to check if the file exists, or if so, it is a valid HDF5 or just give a `try`
      fd = h5::open("some_container.h5",  H5F_ACC_RDWR);
  } catch ( const h5::error::any& err ){
      fd =  h5::create("some_container.h5",H5F_ACC_TRUNC);
  }
  h5:ds_t ds = h5::open(fd, "dataset-name", ...);
  ... do your thing ...: use H5CPP templates or CAPI calls with H5CPP RAII capable descriptors
}

Here are the presentation slides, and the catch of the day: documentation on structured exceptions.

H5CPP may be downloaded from this gihub page.
steve


#5

Hi @mike.izquierdo,

Another way to solve your issue in C++ could be through the usage of HDFql as it considerably softens HDF5 learning curve since it is based on a declarative paradigm (as opposite to imperative).

Your issue could be solved as follows using HDFql in C++:

// test for the existence of file 'my_file.h5'
if (HDFql::execute("SHOW FILE my_file.h5") == HDFql::Success)
{
    // use (i.e. open) file 'my_file.h5' since it does exist
    HDFql::execute("USE FILE my_file.h5");
}
else
{
    // create file 'my_file.h5' since it does not exist and use (i.e. open) it
    HDFql::execute("CREATE AND USE FILE my_file.h5");
}

#6

Hi Mike,
You can see the C++ RM here: https://portal.hdfgroup.org/display/HDF5/HDF5+1.12+CPP+Reference+Manual
In the program that created the file, do you know whether all calls succeeded? If there were failures, the file may not be closed properly, hence, may not exist. Would you be able to check for it in the directory after that program finishes?