/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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-inter.h5" #define DATASETNAME "MoreCompArrayOfStructures" #define RANK 1 int main(void) { /* First structure and dataset*/ typedef struct s1_t { int a; float b; char name[16]; double c; } s1_t; s1_t s1[3] = {{1,2.0,"three",4.0}, {2,3.0,"four",5.0}, {3,4.0,"five",6.0}}; hid_t s1_tid; /* File datatype identifier */ int i; hid_t file, dataset, space,t_string; /* Handles */ herr_t status; hsize_t dim[1]; /* Dataspace dimensions */ /* Create the HDF5 string*/ t_string = H5Tcopy(H5T_C_S1); H5Tset_size(t_string,16); /* * Create the data space. */ dim[0] = 3; space = H5Screate_simple(RANK, dim, NULL); /* * 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, "int_type", HOFFSET(s1_t, a), H5T_NATIVE_INT); H5Tinsert(s1_tid, "float_type", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); H5Tinsert(s1_tid, "string_type",HOFFSET(s1_t,name),t_string); H5Tinsert(s1_tid, "double_type", 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(t_string); H5Tclose(s1_tid); H5Sclose(space); H5Dclose(dataset); H5Fclose(file); return 0; }