/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * This example shows how to create a compound data type, * write an array which has the compound data type to the file, * and read back fields' subsets. */ #include "hdf5.h" #define H5FILE_NAME "comp-scalar.h5" #define DATASETNAME "ArrayOfStructures" #define LENGTH 10 #define RANK 1 int main(void) { /* First structure and dataset*/ typedef struct s1_t { int a; float b; double c; } s1_t; s1_t s1; hid_t s1_tid; /* File datatype identifier */ int i; hid_t file, dataset, space; /* Handles */ herr_t status; hsize_t dim[] = {LENGTH}; /* Dataspace dimensions */ /* * Initialize the data */ s1.a = 0; s1.b = 1.1; s1.c = 0.5; /* * Create the data space. */ space = H5Screate(H5S_SCALAR); /* * Create the file. */ file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create the memory data type. */ s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t)); H5Tinsert(s1_tid, "a_name", HOFFSET(s1_t, a), H5T_NATIVE_INT); H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE); /* * Create the dataset. */ dataset = H5Dcreate(file, DATASETNAME, s1_tid, space, H5P_DEFAULT); /* * Wtite data to the dataset; */ status = H5Dwrite(dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s1); /* * Release resources */ H5Tclose(s1_tid); H5Sclose(space); H5Dclose(dataset); H5Fclose(file); return 0; }