String not written out to Compound datatype


#1

Hi, Sorry if this is the wrong place to ask this, however I’m stuck on this problem of not being able to write out a string to a compound datatype: Code is:

    hid_t      memtype;

    const int  LENGTH = 2;
    const int  RANK = 1;
    int        i;
    hid_t      file, dataset, space, strtype; // Handles 
    herr_t     str;
    hsize_t    dim[] = { LENGTH };   // Dataspace dimensions 

    //Create the data space.
    space = H5Screate_simple(RANK, dim, NULL);

    //Create variable - length string datatype.
    strtype = H5Tcopy(H5T_C_S1);
    str = H5Tset_size(strtype, H5T_C_S1);
    H5::StrType stype(H5::PredType::C_S1, H5T_VARIABLE);
    //H5::StrType(H5::PredType::C_S1, H5T_VARIABLE)

    memtype = H5Tcreate(H5T_COMPOUND, sizeof(entity_data));
    H5Tinsert(memtype, "name", HOFFSET(Entity, name), strtype);
    H5Tinsert(memtype, "entityID", HOFFSET(Entity, entity_id), H5T_NATIVE_INT);
    H5Tinsert(memtype, "parentID", HOFFSET(Entity, parent_id), H5T_NATIVE_INT);

    //Create the dataset.
    dataset = H5Dcreate2(mEntGrpPtr->getLocId(), "EntityData", memtype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  
    
    //Wtite data to the dataset;
    
     H5Dwrite(dataset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &entity_data);

The file output is:

GROUP “Entities” {
DATASET “EntityData” {
DATATYPE H5T_COMPOUND {
H5T_STD_I32LE “entityID”;
H5T_STD_I32LE “parentID”;
}
DATASPACE SIMPLE { ( 2 ) / ( 2 ) }
DATA {
(0): {
1,
0
},
(1): {
15,
0
}
}
}

What am I missing?

All the best,
Paul


#2

Paul, can you show us the definition of entity_data's (Entity?) type? What’s the type of the name field? Why are you mixing two APIs? (What’s the point of H5::StrType?) G.


#3

Hi, thanks for the reply.

my struct looks like this

typedef struct Entity
{
char name[36];

int    entity_id;

int    parent_id;

} Entity;

At the moment I’m trying to use fixed length strings. with char [36].
I’m copying the string values with:

strcpy(entity_data.name,name.c_str());

My current setup is

hid_t dtype = H5Tcopy(H5T_C_S1);
int size = 36;
status = H5Tset_size(dtype, size);

memtype = H5Tcreate(H5T_COMPOUND, sizeof(Entity));
status = H5Tinsert(memtype, “entityID”, HOFFSET(Entity, entity_id), H5T_NATIVE_INT);
status = H5Tinsert(memtype, “name”, HOFFSET(Entity, name), dtype);
status = H5Tinsert(memtype, “parentID”, HOFFSET(Entity, parent_id), H5T_NATIVE_INT);

space = H5Screate_simple(1, dims, NULL);

H5std_string dsetName = “Entity” + std::to_string(entity_data.entity_id);

dset = H5Dcreate(mEntGrpPtr->getLocId(), dsetName.c_str(), memtype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &entity_data);

II’m getting this in the h5 file:

entityID name parentID
1601463658 b’\x01’ 0

when it should be:

entityID name parentID
0 1 jet 0

If I try to setup for variable strings my application crashes.
I’m new to hdf5 and I know I’m setting up the mem space wrong, just need a little help…

Best regards,
Paul


#4

Hi, I fixed it. It was more to do with the way I was creating/copying over strings.

Thanks,
Paul