介绍

官网很多用例测试,但是全英的优点费劲,这里就将示例总结一下,提高用例的可读性,希望能帮助大家快速上手HDF5.

相关函数及作用

函数名作用
H5Fcreate创建HDF5文件
H5Gcreate创建组

用例代码及说明

  • #include <iostream>
  • #include <string>
  • #include "H5Cpp.h"
  • #pragma execution_character_set("utf-8")
  • #define FILE "h5ex_t_cmpd.h5"
  • #define DATASET "DS1"
  • #define DIM0 10
  • using namespace std;
  • struct sensor_r{
  • string location1;
  • string location2;
  • string location3;
  • string location4;
  • string location5;
  • string location6;
  • string location7;
  • int serial_no;
  • string location;
  • double temperature;
  • double pressure;
  • };
  • typedef struct {
  • char *location1;
  • char *location2;
  • char *location3;
  • char *location4;
  • char *location5;
  • char *location6;
  • char *location7;
  • int serial_no;
  • char *location;
  • double temperature;
  • double pressure;
  • } sensor_t; /* Compound type */
  • int
  • main (void)
  • {
  • hid_t file, filetype, memtype, strtype, space, dset;
  • /* Handles */
  • herr_t status;
  • hsize_t dims[1] = {DIM0};
  • int ndims,
  • i;
  • sensor_r rdata[DIM0];
  • for (int i = 0; i < DIM0; i++)
  • {
  • rdata[i].location1 = "test";
  • rdata[i].location2 = "test中文";
  • rdata[i].location3 = "test";
  • rdata[i].location4 = "test";
  • rdata[i].location5 = "test";
  • rdata[i].location6 = "test";
  • rdata[i].location7 = "test";
  • rdata[i].serial_no = i;
  • rdata[i].location = "test123中文";
  • rdata[i].temperature = i;
  • rdata[i].pressure = 2.0;
  • }
  • sensor_t * wdata = new sensor_t[DIM0];
  • for (int i = 0; i < DIM0; i++)
  • {
  • wdata[i].location1 = (char*)rdata[i].location1.c_str();
  • wdata[i].location2 = (char*)rdata[i].location2.c_str();
  • wdata[i].location3 = (char*)rdata[i].location3.c_str();
  • wdata[i].location4 = (char*)rdata[i].location4.c_str();
  • wdata[i].location5 = (char*)rdata[i].location5.c_str();
  • wdata[i].location6 = (char*)rdata[i].location6.c_str();
  • wdata[i].location7 = (char*)rdata[i].location7.c_str();
  • wdata[i].serial_no = i;
  • wdata[i].location = (char*)rdata[i].location.c_str();
  • wdata[i].temperature = i;
  • wdata[i].pressure = 2.0;
  • cout << wdata[i].location2 << endl;
  • }
  • /*
  • * Create a new file using the default properties.
  • */
  • file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  • /*
  • * Create variable-length string datatype.
  • */
  • strtype = H5Tcopy (H5T_C_S1);
  • status = H5Tset_size (strtype, H5T_VARIABLE);
  • /*
  • * Create the compound datatype for memory.
  • */
  • memtype = H5Tcreate (H5T_COMPOUND, sizeof (sensor_t));
  • status = H5Tinsert (memtype, "Location1", HOFFSET (sensor_t, location1),strtype);
  • status = H5Tinsert (memtype, "Location2", HOFFSET (sensor_t, location2),strtype);
  • status = H5Tinsert (memtype, "Location3", HOFFSET (sensor_t, location3),strtype);
  • status = H5Tinsert (memtype, "Location4", HOFFSET (sensor_t, location4),strtype);
  • status = H5Tinsert (memtype, "Location5", HOFFSET (sensor_t, location5),strtype);
  • status = H5Tinsert (memtype, "Location6", HOFFSET (sensor_t, location6),strtype);
  • status = H5Tinsert (memtype, "Location7", HOFFSET (sensor_t, location7),strtype);
  • status = H5Tinsert (memtype, "Serial number",HOFFSET (sensor_t, serial_no), H5T_NATIVE_INT);
  • status = H5Tinsert (memtype, "Location", HOFFSET (sensor_t, location),strtype);
  • status = H5Tinsert (memtype, "Temperature (F)", HOFFSET (sensor_t, temperature), H5T_NATIVE_DOUBLE);
  • status = H5Tinsert (memtype, "Pressure (inHg)", HOFFSET (sensor_t, pressure), H5T_NATIVE_DOUBLE);
  • /*
  • * Create dataspace. Setting maximum size to NULL sets the maximum
  • * size to be the current size.
  • */
  • space = H5Screate_simple (1, dims, NULL);
  • /*
  • * Create the dataset and write the compound data to it.
  • */
  • dset = H5Dcreate (file, DATASET, memtype, space, H5P_DEFAULT, H5P_DEFAULT,
  • H5P_DEFAULT);
  • status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata);
  • /*
  • * Close and release resources.
  • */
  • status = H5Dclose (dset);
  • status = H5Sclose (space);
  • status = H5Tclose (memtype);
  • status = H5Fclose (file);
  • return 0;
  • }

本文由 管理员 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论