NCSA

[ HDF5 Tutorial Top ]

Reading to/Writing from a Dataset


Contents:


Reading to/Writing from a Dataset

During a dataset I/O operation, the library transfers raw data between memory and the file. The memory can have a data type different than the file data type and can also be a different size (memory is a subset of the dataset elements, or vice versa). Therefore, to perform read or write operations, the application program must specify:

The steps to read to/write from a dataset are as follows:

  1. Obtain the dataset identifier.
  2. Specify the memory data type.
  3. Specify the memory dataspace.
  4. Specify the file dataspace.
  5. Specify the transfer properties.
  6. Perform the desired operation on the dataset.
  7. Close the dataset.
  8. Close the dataspace/data type, and property list if necessary.
To read to/write from a dataset, the calling program must contain the following call:
   H5Dread(dataset_id, mem_type_id, mem_space_id, file_space_id,
           xfer_plist_id, buf );
or
   H5Dwrite(dataset_id, mem_type_id, mem_space_id, file_space_id,
            xfer_plist_id, buf);

Programming Example

Description

The following example shows how to read and write an existing dataset. It opens the file created in the previous example, obtains the dataset identifier, /dset, writes the dataset to the file, then reads the dataset back from memory. It then closes the dataset and file.
[
Download h5_rdwt.c ]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include <hdf5.h>
#define FILE "dset.h5"

main() {

   hid_t       file_id, dataset_id;  /* identifiers */
   herr_t      status;
   int         i, j, dset_data[4][6];

   /* Initialize the dataset. */
   for (i = 0; i < 4; i++)
      for (j = 0; j < 6; j++)
         dset_data[i][j] = i * 6 + j + 1;

   /* Open an existing file. */
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

   /* Open an existing dataset. */

   dataset_id = H5Dopen(file_id, "/dset");

   /* Write the dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                     dset_data);

   status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                    dset_data);

   /* Close the dataset. */
   status = H5Dclose(dataset_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Remarks