diff options
author | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2000-11-14 21:36:14 (GMT) |
---|---|---|
committer | Binh-Minh Ribler <bmribler@hdfgroup.org> | 2000-11-14 21:36:14 (GMT) |
commit | 083fa734a39f774224b75702e401c494676c4c93 (patch) | |
tree | 5b8b64ede432af11be3a82181ef7d5455e2aabec /c++/examples/create.C | |
parent | 92041a68656c59813619ae1f26ed211b7f028e86 (diff) | |
download | hdf5-083fa734a39f774224b75702e401c494676c4c93.zip hdf5-083fa734a39f774224b75702e401c494676c4c93.tar.gz hdf5-083fa734a39f774224b75702e401c494676c4c93.tar.bz2 |
[svn-r2898]
Purpose:
C++ API for 1.3.x branch
Description:
The *.C files are the sample code that perform some common
operations to an HDF5 file and its components.
The *.h5 files are the HDF5 files that are generated/used
by the sample programs.
Platforms tested:
Solaris (arabica) and Linux
Diffstat (limited to 'c++/examples/create.C')
-rw-r--r-- | c++/examples/create.C | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/c++/examples/create.C b/c++/examples/create.C new file mode 100644 index 0000000..f17bd09 --- /dev/null +++ b/c++/examples/create.C @@ -0,0 +1,112 @@ +// +// This example writes a dataset to a new HDF5 file. +// + +#include <string> +#ifndef H5_NO_NAMESPACE +using namespace std; +#endif + +#ifdef OLD_HEADER_FILENAME +#include <iostream.h> +#else +#include <iostream> +#endif +#include "H5Cpp.h" + +#ifndef H5_NO_NAMESPACE +using namespace H5; +#endif + +const string FILE_NAME( "SDS.h5" ); +const string DATASET_NAME( "IntArray" ); +const int NX = 5; // dataset dimensions +const int NY = 6; +const int RANK = 2; + +int main (void) +{ + // + // Data initialization. + // + int i, j; + int data[NX][NY]; // buffer for data to write + for (j = 0; j < NX; j++) + { + for (i = 0; i < NY; i++) + data[j][i] = i + j; + } + // + // 0 1 2 3 4 5 + // 1 2 3 4 5 6 + // 2 3 4 5 6 7 + // 3 4 5 6 7 8 + // 4 5 6 7 8 9 + // + + // Try block to detect exceptions raised by any of the calls inside it + try + { + // + // Create a new file using H5F_ACC_TRUNC access, + // default file creation properties, and default file + // access properties. + // + H5File file( FILE_NAME, H5F_ACC_TRUNC ); + + // + // Define the size of the array and create the data space for fixed + // size dataset. + // + hsize_t dimsf[2]; // dataset dimensions + dimsf[0] = NX; + dimsf[1] = NY; + DataSpace dataspace( RANK, dimsf ); + + // + // Define datatype for the data in the file. + // We will store little endian INT numbers. + // + IntType datatype( PredType::NATIVE_INT ); + datatype.setOrder( H5T_ORDER_LE ); + + // + // Create a new dataset within the file using defined dataspace and + // datatype and default dataset creation properties. + // + DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace ); + + // + // Write the data to the dataset using default memory space, file + // space, and transfer properties. + // + dataset.write( data, PredType::NATIVE_INT ); + } // end of try block + + // catch failure caused by the H5File operations + catch( FileIException error ) + { + error.printError(); + } + + // catch failure caused by the DataSet operations + catch( DataSetIException error ) + { + error.printError(); + } + + // catch failure caused by the DataSpace operations + catch( DataSpaceIException error ) + { + error.printError(); + } + + // catch failure caused by the DataSpace operations + catch( DataTypeIException error ) + { + error.printError(); + } + + return 0; // successfully terminated +} + |