HDF5 (Hierarchical Data Format) 由美国伊利诺伊大学厄巴纳-香槟分校UIUC (University of Illinois at Urbana-Champaign) 开发,是一种常见的跨平台数据储存文件,可以存储不同类型的图像和数码数据,并且可以在不同类型的机器上传输,同时还有统一处理这种文件格式的函数库。
intmain(int argc, char **argv) { // Try block to detect exceptions raised by any of the calls inside it. try { /* * Turn off the auto-printing when failure occurs so that we can * handle the errors appropriately. */ Exception::dontPrint();
double data[DIM0][DIM1]; // buffer for data to write
for (int i = 0; i < DIM0; i++) for (int j = 0; j < DIM1; j++) data[i][j] = (i + 1) * PI + j; // Create a new file using the default property lists. // H5::H5F_ACC_TRUNC : create a new file or overwrite an existing file. H5File file(FILE_NAME, H5F_ACC_TRUNC);
// Create a group under root '/'. Group group(file.createGroup(GROUP_NAME)); // Use H5::hsize_t (similar to int) for dimensions. hsize_t dims[RANK]; // dataset dimensions dims[0] = DIM0; dims[1] = DIM1; // Create the dataspace for a dataset first. DataSpace dataspace(RANK, dims); // Create the dataset under group with specified dataspace. DataSet dataset = group.createDataSet(DATASET_NAME, PredType::NATIVE_DOUBLE, dataspace);
// Write data in buffer to dataset. dataset.write(data, PredType::NATIVE_DOUBLE);
int attr1_data[2] = {100, 200}; // buffer for attribute data to wirte
hsize_t attr1_dims[1] = {2}; // attribute dimension, rank = 1 // Create the dataspace for an attribute first. DataSpace attr1_dataspace(1, attr1_dims); // rank = 1
// Create the attribute of dataset with specified dataspace. Attribute attribute1 = dataset.createAttribute(ATTR_NAME1, PredType::STD_I32BE, attr1_dataspace); // Write data in buffer to attribute. attribute1.write(PredType::NATIVE_INT, attr1_data);
// Create the dataspace for an attribute first. DataSpace attr2_dataspace(1, attr2_dims); // rank = 1
// Create the attribute of dataset with specified dataspace. Attribute attribute2 = dataset.createAttribute(ATTR_NAME2, PredType::NATIVE_CHAR, attr2_dataspace); // Write data in buffer to attribute. attribute2.write(PredType::NATIVE_CHAR, attr2_data);
#ifndef _H5_NO_NAMESPACE_ usingnamespace H5; #ifndef _H5_NO_STD_ using std::cout; using std::endl; #endif/* _H5_NO_STD_ */ #endif/* _H5_NO_NAMESPACE_ */
/* * Define the names of HDF5 file, groups, datasets, and attributes. * Use H5::H5std_string for name strings. */ const H5std_string FILE_NAME("h5cpp_example.hdf5"); const H5std_string GROUP_NAME("group1"); const H5std_string DATASET_NAME("dset"); const H5std_string ATTR_NAME("myAttr2");
intmain(int argc, char **argv) { // Try block to detect exceptions raised by any of the calls inside it. try { /* * Turn off the auto-printing when failure occurs so that we can * handle the errors appropriately */ Exception::dontPrint();
/* // Open an existing file. // H5::H5F_ACC_RDWR : read or edit an existing file. H5File file_d(FILE_NAME, H5F_ACC_RDWR); // Open an existing group. Group group_d = file_d.openGroup(GROUP_NAME); // Use H5::H5Ldelete to delete an existing dataset. int result = H5Ldelete(group_d.getId(), DATASET_NAME.c_str(), H5P_DEFAULT); // String.c_str() convert "string" to "const char *". cout << result << endl; // Non-negtive: successfully delete; // Otherwise: fail. // Save and exit the group. group_d.close(); // Save and exit the file. file_d.close(); // Important! The two close()s above can't be omitted! // Otherwise, the deleting behavior won't be saved to file. */
// Open an existing file. // H5::H5F_ACC_RDWR : read or edit an existing file. H5File file(FILE_NAME, H5F_ACC_RDWR); // Open an existing group of the file. Group group = file.openGroup(GROUP_NAME);
// Open an existing dataset of the group. DataSet dataset = group.openDataSet(DATASET_NAME);
// Get the dataspace of the dataset. DataSpace filespace = dataset.getSpace();
// Get the rank of the dataset. int rank = filespace.getSimpleExtentNdims();
// Use H5::hsize_t (similar to int) for dimensions hsize_t dims[rank]; // dataset dimensions
// Get the dimensions of the dataset. rank = filespace.getSimpleExtentDims(dims);