Trouble Reading Fixed Length Strings


#1

Hi,

I’m new to using the HDF5 library and I’m have some trouble reading fixed length strings from a dataset. I am able to read variable length strings ok. The variable length strings have the following format:
DATATYPE H5T_STRING {
STRSIZE H5T_VARIABLE;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_UTF8;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 24 ) / ( 24 ) }
DATA {
(0): “Cut_1”, “Cut_2”, “Cut_3”, “Cut_4”, “Cut_5”, “Cut_6”, “Cut_7”,
(7): “Cut_8”, “Cut_9”, “Cut_10”, “Cut_11”, “Cut_12”, “Cut_13”,
(13): “Cut_14”, “Cut_15”, “Cut_16”, “Cut_17”, “Cut_18”, “Cut_19”,
(19): “Cut_20”, “Cut_21”, “Cut_22”, “Cut_23”, “Cut_24”
}

And I use the following code to read it:
file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dset = H5Dopen(loc_id, name, H5P_DEFAULT);
datatype = H5Dget_type(dset);
dataclass = H5Tget_class(datatype);
space = H5Dget_space(dset);
ndims = H5Sget_simple_extent_ndims(space);
dims = (hsize_t *)malloc(sizeof(hsize_t) * ndims);
H5Sget_simple_extent_dims(space, dims, NULL);

string_data = (char **) malloc (dims[0] * sizeof (char *));
channel_type = H5Tget_native_type(datatype, H5T_DIR_DEFAULT);

status = H5Dread(dset, channel_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, string_data);

The variable length data has the following format:
DATATYPE H5T_STRING {
STRSIZE 31;
STRPAD H5T_STR_NULLTERM;
CSET H5T_CSET_ASCII;
CTYPE H5T_C_S1;
}
DATASPACE SIMPLE { ( 16 ) / ( 16 ) }
DATA {
(0): “Mode-1”, “Mode-2”, “Mode-3”, “Mode-4”, “Mode-5”, “Mode-6”,
(6): “Mode-7”, “Mode-8”, “Mode-9”, “Mode-10”, “Mode-11”, “Mode-12”,
(12): “Mode-13”, “Mode-14”, “Mode-15”, “Mode-16”
}

I’ve tried similar code to read the variable length string array but I always get an error. Can anyone suggest what sequence of calls are required to read this variable length data?


#2

Unlike datasets of vlen strings, which are represented in memory as pointer arrays (char**), datsets of fixed-length strings are represented as one long (padded) sequence of characters (char*). In your example, the string size is 31 and you have 16 elements (strings). char string_data[496]; should work just fine. G.