(Solved) Problem creating HDF5-files via API


i have just compiled HDF5 on my Windows 10, 64-bit system for Visual Studio 2015 and sucessfully integrated the dlls it in a C++ application.

However, i am having trouble following the most basic tutorials, as my calls to H5::H5File fail, regardless of wether i am trying to create a new (empty) hdf-file or for reading an existing sample file.

#include <3rdParty/HDF5/include/H5Cpp.h>
H5::H5std_string	filename("h5tutr_dset.h5");
H5::H5File file(filename, H5F_ACC_TRUNC);  

the last line throws FileIException with Fuction name “H5File constructor” and Detail-Message “H5FCreate failed”

How do I continue investigating what goes wrong here?
In which location will the file be written?

Hi Jochmann,

The line:

H5::H5File file(filename, H5F_ACC_TRUNC);

will try to overwrite h5tutr_dset.h5 if the file already exists. Please make sure that the file is not a read-only file. You can also change the flag to H5F_ACC_RDONLY if you just want to open and read it.

The file definitely does not exist anywhere on my filesystem, so thats not the problem.
Is there a kind of “beginner problem checklist” that I can refer to? These are my first steps with HDF5

Ok, solved it. The problem was that I was using a debug-build of my application, linking against a release-build of HDF5. Using a release build of my application did not have the problem. (Cause: std::string has a different memory layout in debug and releae, and that generated invalid or very funky filenames)

Notes to self and for future reference:

  • Preferrably: do not use mixed builds. This is a bad idea and can cause a lot of problems. Compile a debug version of HDF5 for use with debug builds of the application
  • In mixed builds, do not pass std::string between applications. use the char* versions or std::string::c_str() instead

Noticed that you’re working with C++, you might want to check out H5CPP and it’s examples directory.
Although windows is not yet supported the header only library is confirmed to work, the only requirement is to link against HDF5 1.10.4 CAPI[^1]. For now the source code transformation tool requires linux to run however the generated code has no additional dependencies.

The project supports major linear algebra libraries and arbitrary POD struct types, easy pythonic syntax through template meta programming and good performance characteristics on par of CAPI, a new high performance packet table on par with the underlying file system throughput or 300x faster of the one provided in high level api.

Here is a short example to store armadillo matrix in HDF5, an easy data interop between C++ and scientific platforms such as scipy,julia,matlab,R, …

#include <armadillo>
#include <h5cpp/all>

int main(){
   arma::mat M(2,3);
   // one shot approach will do the right thing, no need to read pages of documentation
   h5::write("01.h5","single shot write", M);

  // or you already know what hdf5 can do and would like to go full throttle
   // create an HDF5 container/file and hold on to desccriptor
   h5::fd_t fd = h5::create("02.h5",H5F_ACC_TRUNC);
   // get a dataset descriptor by creating a dataset
   h5::ds_t ds = h5::create<short>(fd,"create then write"
      ,h5::chunk{2,3} | h5::fill_value<short>{3} |  h5::gzip{9});
   // call write -- handy within loops
   h5::write( ds,  M, h5::offset{2,2}, h5::stride{1,3}  );
   // scalable structured error handling with h5::error::???::any construct 
   } catch( const h5::error::io::any& err ){
// RAII closes  'fd' and 'ds' descriptors