Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. /************************************************************
  2.  
  3. This example shows how to read and write variable-length
  4. string datatypes to an attribute. The program first
  5. writes variable-length strings to an attribute with a
  6. dataspace of DIM0, then closes the file. Next, it reopens
  7. the file, reads back the data, and outputs it to the
  8. screen.
  9.  
  10. This file is intended for use with HDF5 Library version 1.6
  11.  
  12. ************************************************************/
  13.  
  14. #include "hdf5.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #define FILE "h5ex_t_vlstringatt.h5"
  19. #define DATASET "DS1"
  20. #define ATTRIBUTE "A1"
  21. #define DIM0 4
  22.  
  23. int
  24. main (void)
  25. {
  26. hid_t file, filetype, memtype, space, dset, attr;
  27. /* Handles */
  28. herr_t status;
  29. hsize_t dims[1] = {DIM0};
  30. char *wdata[DIM0] = {"Parting", "is such", "sweet", "sorrow."},
  31. /* Write buffer */
  32. **rdata; /* Read buffer */
  33. int ndims,
  34. i;
  35.  
  36. /*
  37. * Create a new file using the default properties.
  38. */
  39. file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  40.  
  41. /*
  42. * Create file and memory datatypes. For this example we will save
  43. * the strings as FORTRAN strings.
  44. */
  45. filetype = H5Tcopy (H5T_FORTRAN_S1);
  46. status = H5Tset_size (filetype, H5T_VARIABLE);
  47. memtype = H5Tcopy (H5T_C_S1);
  48. status = H5Tset_size (memtype, H5T_VARIABLE);
  49.  
  50. /*
  51. * Create dataset with a scalar dataspace.
  52. */
  53. space = H5Screate (H5S_SCALAR);
  54. dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  55. status = H5Sclose (space);
  56.  
  57. /*
  58. * Create dataspace. Setting maximum size to NULL sets the maximum
  59. * size to be the current size.
  60. */
  61. space = H5Screate_simple (1, dims, NULL);
  62.  
  63. /*
  64. * Create the attribute and write the variable-length string data
  65. * to it.
  66. */
  67. attr = H5Acreate (dset, ATTRIBUTE, filetype, space, H5P_DEFAULT, H5P_DEFAULT);
  68. status = H5Awrite (attr, memtype, wdata);
  69.  
  70. /*
  71. * Close and release resources.
  72. */
  73. status = H5Aclose (attr);
  74. status = H5Dclose (dset);
  75. status = H5Sclose (space);
  76. status = H5Tclose (filetype);
  77. status = H5Tclose (memtype);
  78. status = H5Fclose (file);
  79.  
  80.  
  81. /*
  82. * Now we begin the read section of this example. Here we assume
  83. * the attribute has the same name and rank, but can have any size.
  84. * Therefore we must allocate a new array to read in data using
  85. * malloc().
  86. */
  87.  
  88. /*
  89. * Open file, dataset, and attribute.
  90. */
  91. file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
  92. dset = H5Dopen (file, DATASET, H5P_DEFAULT);
  93. attr = H5Aopen_name (dset, ATTRIBUTE);
  94.  
  95. /*
  96. * Get the datatype.
  97. */
  98. filetype = H5Aget_type (attr);
  99.  
  100. /*
  101. * Get dataspace and allocate memory for read buffer.
  102. */
  103. space = H5Aget_space (attr);
  104. ndims = H5Sget_simple_extent_dims (space, dims, NULL);
  105. rdata = (char **) malloc (dims[0] * sizeof (char *));
  106.  
  107. /*
  108. * Create the memory datatype.
  109. */
  110. memtype = H5Tcopy (H5T_C_S1);
  111. status = H5Tset_size (memtype, H5T_VARIABLE);
  112.  
  113. /*
  114. * Read the data.
  115. */
  116. status = H5Aread (attr, memtype, rdata);
  117.  
  118. /*
  119. * Output the data to the screen.
  120. */
  121. for (i=0; i<dims[0]; i++)
  122. printf ("%s[%d]: %s\n", ATTRIBUTE, i, rdata[i]);
  123.  
  124. /*
  125. * Close and release resources. Note that H5Dvlen_reclaim works
  126. * for variable-length strings as well as variable-length arrays.
  127. * Also note that we must still free the array of pointers stored
  128. * in rdata, as H5Tvlen_reclaim only frees the data these point to.
  129. */
  130. status = H5Dvlen_reclaim (memtype, space, H5P_DEFAULT, rdata);
  131. free (rdata);
  132. status = H5Aclose (attr);
  133. status = H5Dclose (dset);
  134. status = H5Sclose (space);
  135. status = H5Tclose (filetype);
  136. status = H5Tclose (memtype);
  137. status = H5Fclose (file);
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement