1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  *  This example illustrates how to write to and read from an existing
16  *  dataset. It is used in the HDF5 Tutorial.
17  */
18 
19 #include <iostream>
20 #include <string>
21 #include "H5Cpp.h"
22 using namespace H5;
23 
24 const H5std_string      FILE_NAME("h5tutr_dset.h5");
25 const H5std_string      DATASET_NAME("dset");
26 const int       DIM0 = 4;                      // dataset dimensions
27 const int       DIM1 = 6;
28 
main(void)29 int main (void)
30 {
31 
32     // Data initialization.
33 
34     int i, j;
35     int data[DIM0][DIM1];           // buffer for data to write
36 
37     for (j = 0; j < DIM0; j++)
38         for (i = 0; i < DIM1; i++)
39          data[j][i] = i * 6 + j + 1;
40 
41     // Try block to detect exceptions raised by any of the calls inside it
42     try
43     {
44         // Turn off the auto-printing when failure occurs so that we can
45         // handle the errors appropriately
46         Exception::dontPrint();
47 
48         // Open an existing file and dataset.
49         H5File file(FILE_NAME, H5F_ACC_RDWR);
50         DataSet dataset = file.openDataSet(DATASET_NAME);
51 
52         // Write the data to the dataset using default memory space, file
53         // space, and transfer properties.
54         dataset.write(data, PredType::NATIVE_INT);
55 
56     }  // end of try block
57 
58     // catch failure caused by the H5File operations
59     catch(FileIException error)
60     {
61         error.printErrorStack();
62         return -1;
63     }
64 
65     // catch failure caused by the DataSet operations
66     catch(DataSetIException error)
67     {
68         error.printErrorStack();
69         return -1;
70     }
71 
72     return 0;  // successfully terminated
73 }
74