Opening an HDF5 for Update


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?


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


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.



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.


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");
    // 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");


Hi Mike,
You can see the C++ RM here:
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?