Creating A Simple Dataset C++ Invalid Location Identifier


I’m new to hdf5 I’m trying to get to grips with it by following some examples from this page:

I’m using c++ (c++11) and CentOs7 (CentOS Linux release 7.9.2009 (Core), gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44))
The code is shown below:

//set up HDF5 file here                                                                                           
hid_t       file_id;
herr_t      status;
file_id = H5Fcreate ("file.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
status = H5Fclose (file_id);

//Create the dataspace for the dataset.                                                                         
hsize_t dims[2];
dims[0] = 4;
dims[1] = 6;
hid_t dataspace_id = H5Screate_simple(2, dims, NULL);

// Create the dataset.                                                                                          
hid_t dataset_id = H5Dcreate (file_id, "/dset", H5T_STD_I32BE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

// Close the dataset and dataspace                                                                              
status = H5Dclose(dataset_id);
status = H5Sclose(dataspace_id);

As I understand it this should be a simple process but I get the following error:
HDF5-DIAG: Error detected in HDF5 (1.12.1) thread 0:
#000: /user/rcollins/programs/hdf5-1.12.1/src/H5D.c line 132 in H5Dcreate2(): invalid location identifier
major: Invalid arguments to routine
minor: Inappropriate type
HDF5-DIAG: Error detected in HDF5 (1.12.1) thread 0:
#000: /user/rcollins/programs/hdf5-1.12.1/src/H5D.c line 320 in H5Dclose(): not a dataset ID
major: Invalid arguments to routine
minor: Inappropriate type

This is very confusing to me, browsing an old thread apparently this could be a memory issue. With such a simple example how is that the case? Have I made a basic error somewhere? The issue seems to be in H5Dcreate and H5Dclose() but everything else seems to be working fine I can see that a file called “file.h5” is written to disk. Any help would be greatly appreciated.

Kind regards, Ron.


Hi @snillocdlanor,

Being new to HDF5 is really challenging mainly due to its learning curve being quite steep.

That said, to lower this steepness, you may want to check HDFql, especially if you already know some SQL. HDFql is a high-level (declarative) language that effectively abstracts users from the low-level details of dealing with HDF5.

Looking at the code snippet you have posted, it could be converted in C++ using HDFql as follows (so that it may illustrate how easy it is to use this approach):

// create an HDF5 file named 'file.h5' (truncate it if already existing) 
HDFql::execute("CREATE TRUNCATE file file.h5");

// create a 2D array dataset named 'dset' (in file 'file.h5') of integers
HDFql::execute("CREATE DATASET file.h5 dset AS INT(4, 6)");

Hope it helps!


You may be interested in H5CPP an easy to use template meta programming based HDF5 persistence with compiler assisted static reflection. The project is continuously being developed aiming for the same convenience you get from interpreted languages with the guaranteed performance properties of C++.
You find the presentations slides here if you have any questions ask away on this forum.
best: steve


You are closing the file right after creation.

That’s not gonna fly, because file_id is invalid by the time

rolls around. Moving status = H5Fclose (file_id); to the bottom of your program should get you going.



Interesting I will give it a look.


Thank you that did the trick! I can’t believe I didn’t see that.


This might be useful. I’m a little unsure if its exactly what I want but I’ll keep it mind moving forward. Thanks for the responce!