/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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 "comp3.h5" #define DATASETNAME "MoreCompArrayOfStructures2" #define RANK 1 int main(void) { /* First structure and dataset*/ typedef struct { int a; float b; char name[17]; double c; } s1_t; typedef struct { int a; s1_t e; char name[5]; } s2_t; s2_t s2[3]; 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,s2_tid; /* File datatype identifier */ int i; hid_t file, dataset, space,t_str1,t_str2; /* Handles */ herr_t status; hsize_t dim[1]; /* Dataspace dimensions */ char *s2_name[] = { "Jan", "Feb", "Mar", NULL }; for (i=0;i<3;i++){ s2[i].a=i; s2[i].e=s1[i]; strncpy(s2[i].name,s2_name[i],sizeof(s2_name[i])); } /* Create two HDF5 strings. */ t_str1 = H5Tcopy(H5T_C_S1); H5Tset_size(t_str1,17); t_str2 = H5Tcopy(H5T_C_S1); H5Tset_size(t_str2,5); /* * 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, "inner_int", HOFFSET(s1_t, a), H5T_NATIVE_INT); H5Tinsert(s1_tid, "inner_float", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); H5Tinsert(s1_tid, "inner_name",HOFFSET(s1_t,name),t_str1); s2_tid = H5Tcreate (H5T_COMPOUND, sizeof(s2_t)); H5Tinsert(s2_tid,"outer_int",HOFFSET(s2_t,a),H5T_NATIVE_INT); H5Tinsert(s2_tid,"outer_com",HOFFSET(s2_t,e),s1_tid); H5Tinsert(s2_tid,"out_name",HOFFSET(s2_t,name),t_str2); /* * Create the dataset. */ dataset = H5Dcreate(file, DATASETNAME, s2_tid, space, H5P_DEFAULT); /* * Wtite data to the dataset; */ status = H5Dwrite(dataset, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s2); /* * Release resources */ H5Tclose(s2_tid); H5Tclose(s1_tid); H5Tclose(t_str1); H5Tclose(t_str2); H5Sclose(space); H5Dclose(dataset); H5Fclose(file); return 0; }