Skip to content

Instantly share code, notes, and snippets.

@aitatanit
Forked from YukiSakamoto/hdf5_simple.cpp
Created June 3, 2014 14:17
Show Gist options
  • Save aitatanit/5a6b2d348bbae79595f6 to your computer and use it in GitHub Desktop.
Save aitatanit/5a6b2d348bbae79595f6 to your computer and use it in GitHub Desktop.
#include <string>
#include <iostream>
#include "H5Cpp.h"
#define MAX_NAME_LENGTH 32
const std::string FileName("SimpleCompound.h5");
const std::string DatasetName("PersonalInformation");
const std::string member_age("Age");
const std::string member_sex("Sex");
const std::string member_name("Name");
const std::string member_height("Height");
typedef struct {
int age;
char sex;
char name[MAX_NAME_LENGTH];
float height;
} PersonalInformation;
int main(void)
{
// Data to write
PersonalInformation person_list[] = {
{ 18, 'M', "Mary", 152.0 },
{ 32, 'F', "Tom", 178.6 },
{ 29, 'M', "Tarou", 166.6 }
};
// the length of the data
int length = sizeof(person_list) / sizeof(PersonalInformation);
// the array of each length of multidimentional data.
hsize_t dim[1];
dim[0] = sizeof(person_list) / sizeof(PersonalInformation);
// the length of dim
int rank = sizeof(dim) / sizeof(hsize_t);
// defining the datatype to pass HDF55
H5::CompType mtype(sizeof(PersonalInformation));
mtype.insertMember(member_age, HOFFSET(PersonalInformation, age), H5::PredType::NATIVE_INT);
mtype.insertMember(member_sex, HOFFSET(PersonalInformation, sex), H5::PredType::C_S1);
mtype.insertMember(member_name, HOFFSET(PersonalInformation, name), H5::StrType(H5::PredType::C_S1, MAX_NAME_LENGTH));
mtype.insertMember(member_height, HOFFSET(PersonalInformation, height), H5::PredType::NATIVE_FLOAT);
// preparation of a dataset and a file.
H5::DataSpace space(rank, dim);
H5::H5File *file = new H5::H5File(FileName, H5F_ACC_TRUNC);
H5::DataSet *dataset = new H5::DataSet(file->createDataSet(DatasetName, mtype, space));
// Write
dataset->write(person_list, mtype);
delete dataset;
delete file;
return 0;
}
all:
g++ hdf5_simple.cpp -lhdf5 -lhdf5_cpp -o hdf5_simple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment