All,
My aim is to write an array of strings ( created dynamically into a
dataset (1D dataset ). I have a variable char ** strings defined .
Before writing this array is populated as given below .
Based on length strings = new char*[length];
Fill each strings[i] .
The dataset consists of a compound datatype wherein one of the
components is a variable length string array .
type = H5Tcopy (H5T_C_S1);
ret = H5Tset_size (type, H5T_VARIABLE);
//One of the components of the datatype is - comType has other
components as well
H5Tinsert(comType,"var str",0,type);
H5Dwrite writes all the strings to the given file as below in hdf5 file
( output from h5dump )
"\320\310\362\0138\312\362\013\350\312\362\013\230\314\362\013\370\314\3
62\013H\315\362\013\375\375\375\375\253\253\253\253\253\253\253\253\356\
376\356\376"
The number of strings written were 6. I stored 6 also a a separate value
in the dataset
When reading the length is read first and memory allocated as
strings = new char*[length];
Then H5Dread is used to read in the value from hdf5 file . I get only
junk values and not the one that is written .
I also tried to modify one of the existing examples . But no success .
#ifdef __cplusplus
extern "C" {
#endif
#include "stdafx.h"
#include "..\..\hdf5\hdf5\hdf5-1.6.5_PC\src\hdf5.h"
//C:\HDF5\hdf5\hdf5\hdf5-1.6.5_PC\src
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#define VFILE "vlstra.h5"
#define VRANK 1
#define VDIM 4
static void write_vlstr_attr ()
{
hid_t file, root, dataspace, att;
hid_t type;
herr_t ret;
hsize_t dims[]={1};
const char *string_att[VDIM] = {"string1",
"string2",
"string3",
"string4", };
char **str;
file = H5Fcreate(VFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
// printf ("H5Fcreate returns: %i\n", file);
/* Create a datatype to refer to. */
type = H5Tcopy (H5T_C_S1);
// printf ("H5Tcopy returns: %i\n", type);
ret = H5Tset_size (type, H5T_VARIABLE);
// printf ("H5Tset_size returns: %i\n", ret);
hid_t strType = H5Tcreate(H5T_COMPOUND,sizeof(str));
H5Tinsert(strType,"var str",0,type);
root = H5Gopen(file, "/");
// printf ("H5Gopen returns: %i\n", root);
dataspace = H5Screate_simple(VRANK, dims, NULL);
// printf ("H5Screate returns: %i\n", dataspace);
str = new char*[4];
for(int j=0;j<4 ; j++)
{
str[j] = new char[8];
strcpy(str[j],string_att[j]);
}
att = H5Dcreate(root, "VarStrAtt", strType, dataspace, H5P_DEFAULT);
// printf ("H5Acreate returns: %i\n", att);
ret = H5Dwrite(att, strType,dataspace,dataspace, H5P_DEFAULT, str);
// printf ("H5Awrite returns: %i\n", ret);
ret = H5Dclose(att);
// printf ("H5Aclose returns: %i\n", ret);
ret = H5Gclose(root);
// printf ("H5Gclose returns: %i\n", ret);
ret = H5Tclose(type);
// printf ("H5Tclose returns: %i\n", ret);
ret = H5Sclose(dataspace);
// printf ("H5Sclose returns: %i\n", ret);
ret = H5Fclose(file);
// printf ("H5Fclose returns: %i\n", ret);
}
static void read_vlstr_attr ()
{
hid_t file, root, att;
hid_t type;
herr_t ret;
char* string_att[VDIM];
char **str;
int i;
hsize_t dims[]={1};
file = H5Fopen(VFILE, H5F_ACC_RDONLY, H5P_DEFAULT);
// printf (""H5Fopen returns: %i\n", file);
/* Create a datatype to refer to. */
type = H5Tcopy (H5T_C_S1);
hid_t strType = H5Tcreate(H5T_COMPOUND,sizeof(str));
H5Tinsert(strType,"var str",0,type);
ret = H5Tset_size (type, H5T_VARIABLE);
// printf (""H5Tset_size returns: %i\n", ret);
root = H5Gopen(file, "/");
// printf (""H5Gopen returns: %i\n", root);
att = H5Dopen(root, "VarStrAtt");
// printf (""H5Aopen_name returns: %i\n", att);
str = new char*[4];
hid_t dataspace = H5Screate_simple(VRANK, dims, NULL);
ret = H5Dread(att, strType, dataspace, H5Dget_space(att),
H5P_DEFAULT, str);
//for (i=0; i<VDIM; i++)
// printf ("" (%i) %s\n", i, string_att[i]);
// printf (""\n");
free(string_att);
ret = H5Dclose(att);
// printf (""H5Aclose returns: %i\n", ret);
ret = H5Tclose(type);
// printf (""H5Tclose returns: %i\n", ret);
ret = H5Gclose(root);
// printf (""H5Gclose returns: %i\n", ret);
ret = H5Fclose(file);
// printf (""H5Fclose returns: %i\n", ret);
}
int _tmain(int argc, _TCHAR* argv[])
{
write_vlstr_attr();
read_vlstr_attr();
return 0;
}