How to create a group in a nested for loop using the C API?

I am trying to learn about the C API and create nested groups using nested for loops, but the program is failing.

Here is the code:

#include "hdf5.h"
#include <string> 
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<string> firstIDs =  {"A", "B", "C"}; 
    vector<string> secondIDs =  {"1", "2", "3"}; 

    hid_t file_id; 
    file_id = H5Fcreate ("RBF_RANDOM_POINTS.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
    for (const auto& firstID : firstIDs)
    {
        for (const auto& secondID : secondIDs)
        {
            // Create the HDF5 group for each RBF and random test size. 
            std::string groupName {
                "/FIRST/" + firstID + "/SECOND/" + secondID 
            };
            hid_t group_id; 
            cout << groupName << endl;
            group_id = H5Gcreate(file_id, groupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
            H5Gclose (group_id);
        }
    }

    H5Fclose(file_id); 
};

and here is the error:

/FIRST/A/SECOND/1
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:
  #000: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5G.c line 323 in H5Gcreate2(): unable to create group
    major: Symbol table
    minor: Unable to initialize object
  #001: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gint.c line 157 in H5G__create_named(): unable to create and link to group
    major: Symbol table
    minor: Unable to initialize object
  #002: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5L.c line 1572 in H5L_link_object(): unable to create new link to object
    major: Links
    minor: Unable to initialize object
  #003: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5L.c line 1813 in H5L__create_real(): can't insert link
    major: Links
    minor: Unable to insert object
  #004: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gtraverse.c line 851 in H5G_traverse(): internal path traversal failed
    major: Symbol table
    minor: Object not found
  #005: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5Gtraverse.c line 741 in H5G__traverse_real(): component not found
    major: Symbol table
    minor: Object not found
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:
  #000: /.cache/pacaur/hdf5-openmpi-java/src/hdf5-1.10.4/src/H5G.c line 682 in H5Gclose(): not a group
    major: Invalid arguments to routine
    minor: Inappropriate type
/FIRST/A/SECOND/2
HDF5-DIAG: Error detected in HDF5 (1.10.4) thread 0:

What is happening? If I use the H5Gcreate outside of the nested loop like in the C API example for group creation, it works.

I am on Linux (Arch), using g++ (GCC) 8.2.1 20180831, I compiled the program with h5c++, and I am using hdf5-openmpi-java 1.10.4-1 package on Arch Linux.

By default, absent intermediate groups don’t get created automatically.
Use H5Pset_create_intermediate_group to construct a link creation property
list (outside the loop) that you’ll pass to H5Gcreate (instead of the first H5P_DEFAULT). Call H5Pclose on that lcpl afterwards.

G.

Thanks! This worked!

Here is the working example:

#include "hdf5.h"
#include <string> 
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<string> firstIDs =  {"A", "B", "C"}; 
    vector<string> secondIDs =  {"1", "2", "3"}; 

    hid_t file_id; 
    file_id = H5Fcreate ("RBF_RANDOM_POINTS.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

    // Link creation property list (lcpl_id).
    hid_t lcpl_id; 

    // Create a link creation property list. 
    lcpl_id = H5Pcreate(H5P_LINK_CREATE);

    // Enable the creation of intermediate group in the link property list.
    H5Pset_create_intermediate_group(lcpl,1);

    for (const auto& firstID : firstIDs)
    {
        for (const auto& secondID : secondIDs)
        {
            // Create the HDF5 group for each RBF and random test size. 
            std::string groupName {
                "/FIRST/" + firstID + "/NPOINTS/" + secondID 
            };
            hid_t group_id; 
            cout << groupName << endl;
            // Create the nested group and all intermediate groups.
            group_id = H5Gcreate(file_id, groupName.c_str(), lcpl, H5P_DEFAULT, H5P_DEFAULT);
            // Close the group.
            H5Gclose (group_id);
        }
    }

    // Close the property list. 
    H5Pclose(lcpl); 
    // Close the file.
    H5Fclose(file_id); 
};

The official example can be found here.

The documentation on the H5Pset_create_intermediate_group can be foundhere.