/************************************************************ This example writes a variable length string, reads it back, and then appends to it. ************************************************************/ #include "hdf5.h" #include #include #include #define FILE "vs2x.h5" #define DATASET "DS1" #define DIM0 4 int main (void) { hid_t file, memtype, space, dset; /* Handles */ herr_t status; char *wdata = "Parting"; char *rdata; /* Read buffer */ char *data; size_t rsize; /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); memtype = H5Tcopy (H5T_C_S1); status = H5Tset_size (memtype, H5T_VARIABLE); space = H5Screate (H5S_SCALAR); /* * Create the dataset and write the variable-length string data to * it. */ dset = H5Dcreate (file, DATASET, memtype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Write string to dataset */ status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wdata); /* Read string back */ status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &rdata); rsize = strlen (rdata)+1; printf ("Size of String Read From Dataset=%i\n", rsize); printf ("String Read From Dataset=%s\n", rdata); /* Append data to string and write it back*/ data = (char *)calloc ((rsize+22), sizeof (char)); strcpy (data, (const char *)rdata); strcat (data, " is such sweet sorrow"); printf( "New String to Write to Dataset = %s\n", data); status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data); /* * Close and release resources. */ status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (memtype); status = H5Fclose (file); }