#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif // H5_NO_STD
#endif
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
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);
}
Exception::dontPrint();
hsize_t dim[] = {LENGTH};
DataSpace space( RANK, dim );
H5File* file = new H5File( FILE_NAME, H5F_ACC_TRUNC );
CompType mtype1( sizeof(s1_t) );
mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT);
mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE);
mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);
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) );
mtype2.insertMember( MEMBER3, HOFFSET(s2_t, c), PredType::NATIVE_DOUBLE);
mtype2.insertMember( MEMBER1, HOFFSET(s2_t, a), PredType::NATIVE_INT);
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) );
mtype3.insertMember( MEMBER2, 0, PredType::NATIVE_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.printError();
return -1;
}
catch( DataSetIException error )
{
error.printError();
return -1;
}
catch( DataSpaceIException error )
{
error.printError();
return -1;
}
catch( DataTypeIException error )
{
error.printError();
return -1;
}
return 0;
}