#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include "H5Cpp.h"
const H5std_string FILE_NAME("SDScompound.h5");
const H5std_string DATASET_NAME("ArrayOfStructures");
const H5std_string MEMBER1("a_name");
const H5std_string MEMBER2("b_name");
const H5std_string MEMBER3("c_name");
const int LENGTH = 10;
const int RANK = 1;
int
main(void)
{
typedef struct s1_t {
int a;
float b;
double c;
} s1_t;
typedef struct s2_t {
double c;
int a;
} s2_t;
try {
int i;
s1_t s1[LENGTH];
for (i = 0; i < LENGTH; i++) {
s1[i].a = i;
s1[i].b = i * i;
s1[i].c = 1. / (i + 1);
}
hsize_t dim[] = {LENGTH};
DataSpace space(RANK, dim);
H5File *file = new H5File(FILE_NAME, H5F_ACC_TRUNC);
CompType mtype1(sizeof(s1_t));
DataSet *dataset;
dataset = new DataSet(file->createDataSet(DATASET_NAME, mtype1, space));
dataset->write(s1, mtype1);
delete dataset;
delete file;
file = new H5File(FILE_NAME, H5F_ACC_RDONLY);
dataset = new DataSet(file->openDataSet(DATASET_NAME));
CompType mtype2(sizeof(s2_t));
s2_t s2[LENGTH];
dataset->read(s2, mtype2);
cout << endl << "Field c : " << endl;
for (i = 0; i < LENGTH; i++)
cout << s2[i].c << " ";
cout << endl;
cout << endl << "Field a : " << endl;
for (i = 0; i < LENGTH; i++)
cout << s2[i].a << " ";
cout << endl;
CompType mtype3(sizeof(float));
float s3[LENGTH];
dataset->read(s3, mtype3);
cout << endl << "Field b : " << endl;
for (i = 0; i < LENGTH; i++)
cout << s3[i] << " ";
cout << endl;
delete dataset;
delete file;
}
catch (FileIException error) {
error.printErrorStack();
return -1;
}
catch (DataSetIException error) {
error.printErrorStack();
return -1;
}
catch (DataSpaceIException error) {
error.printErrorStack();
return -1;
}
catch (DataTypeIException error) {
error.printErrorStack();
return -1;
}
return 0;
}
static void dontPrint()
Turns off the automatic error printing from the C library.
Definition: H5Exception.cpp:161
static const PredType & NATIVE_FLOAT
Definition: H5PredType.h:157
static const PredType & NATIVE_INT
Definition: H5PredType.h:151
static const PredType & NATIVE_DOUBLE
Definition: H5PredType.h:158
Definition: H5AbstractDs.cpp:34