目录
本文基于HDF5官方库,封装了一个简单的常用数据类型的库,HDF5支持的数据类型很多,其中的功能也很强大,但是日常开发中可能常用的包括int、double、string等数据,所以为了简化官方库的使用,自己弄了个小玩意,水平不足,还请见谅。
Visual Studio2013+配置HDF5环境,也可以直接源码和HDF5库CMake编译。
如何配置HDF5开发环境请参考我的另一篇文章
1.HDF5写int,double,string类型的数据到hdf5文件中;
2.读hdf5文件中int,double,string的数据到程序的数据结构中;
3.写数据生成的hdf5文件名使用当前的时间戳来命名。
创建了类来实现相关功能,完整代码见:
- #include "Tool.h"
- #include "Hdf5Function.h"
- using namespace std;
-
- Hdf5WriteValue::Hdf5WriteValue(){}; //构造函数
- Hdf5WriteValue::~Hdf5WriteValue(){};//析构函数
-
- /*创建HDF5*/
- void Hdf5WriteValue::CreateNewFile()
- {
- this->file=H5Fcreate(hdf5_filename().c_str,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
- //H5F_ACC_TRUNC能覆盖,H5F_ACC_EXCL不能覆盖
-
- }
-
- /*创建HDF5 String数据维度*/
- void Hdf5WriteValue::CreateStringDataspace(const void *data,int rank,int col,int row)
- {
- hsize_t dim[2];
- dim[0]=row;
- dim[1]=col;
- size_t size=sizeof(data)/sizeof(char);
- this->status=H5Test_size(dtype,size)
- this->dataspace=H5Screate_simple(rank,dim,NULL);
- }
-
- /*创建HDF5数据维度*/
- void Hdf5WriteValue::CreateDataspace(int rank,int col,int row)
- {
- hsize_t dim[2];
- dim[0]=row;
- dim[1]=col;
- this->dataspace=H5Screate_simple(rank,dim,NULL);
- }
-
- /*创建group*/
- void Hdf5WriteValue::CreateGroup(string groupName)
- {
- this->group=H5Gcreate(this->file,groupName.c_str(),H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
- }
-
- /*创建dataset,int,double,string*/
- void Hdf5WriteValue::CreateIntDataset(string datasetName)
- {
- this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),H5T_NATIVE_INT,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
- }
-
- void Hdf5WriteValue::CreateDoubleDataset(string datasetName)
- {
- this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),H5T_NATIVE_Double,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
- }
-
- void Hdf5WriteValue::CreateStringDataset(string datasetName)
- {
- this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),this->dtype,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
- }
-
- /*写入数据到对应类型的dataset*/
- void Hdf5WriteValue::WriteIntValue(const void *data)
- {
- this->status=H5Dwrite(this->dataset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
- }
-
- void Hdf5WriteValue::WriteDoubleValue(const void *data)
- {
- this->status=H5Dwrite(this->dataset,H5T_NATIVE_DOUNLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
- }
-
- void Hdf5WriteValue::WriteStringValue(const void *data)
- {
- this->status=H5Dwrite(this->dataset,this->dtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
- }
- /*关闭数据及文件,注意关闭顺序*/
- void Hdf5WriteValue::CloseFile()
- {
- this->status=H5Dclose(this->dataset);
- this->status=H5Sclose(this->dataspace);
- this->status=H5Gclose(this->group);
- this->status=H5Fclose(this->file);
- }
- #include "Tools.h"
- #include "Hdf5Function.h"
- using namespace std;
-
- Hdf5ReadValue::Hdf5ReadValue(){}; //构造函数
- Hdf5ReadValue::~Hdf5ReadValue(){};//析构函数
-
- /*打开HDF5文件*/
- void Hdf5ReadValue::OpenFile(const char* filepath)
- {
- this->file = H5Fopen(filepath, H5F_ACC_RDONLY, H5P_DEFAULT);
- }
-
- /*打开HDF5文件对应的Group*/
- void Hdf5ReadValue::OpenGroup(const char* group)
- {
- this->group = H5Gopen2(this->file, group, H5P_DEFAULT);
- }
-
- /*打开HDF5文件对应的Dataset*/
- void Hdf5ReadValue::OpenDataset(const char* dataset)
- {
- this->dataset = H5Dopen(this->group, dataset, H5P_DEFAULT);
- }
-
- /*读文件中的各种类型数据int,double,string*/
- void Hdf5ReadValue::ReadIntData(void *data)
- {
- this->status = H5Dread(this->OpenDataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
- }
-
- void Hdf5ReadValue::ReadDoubleData(void *data)
- {
- this->status = H5Dread(this->OpenDataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
- }
-
- void Hdf5ReadValue::ReadStringData(void *data)
- {
- size_t size = sizeof(data) / sizeof(char);
- this->status = H5Test_size(dtype, size);
- this->status = H5Dread(this->OpenDataset, this->dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
- }
-
- /*关闭数据及文件*/
- void Hdf5ReadValue::CloseFile()
- {
- this->status = H5Dclose(this->dataset);
- this->status = H5Gclose(this->group);
- this->status = H5Fclose(this->file);
- }
- #include
- #include
- #include
- #include "hdf5.h"
-
- #ifndef HDF5FUNCTION_H_
- #define HDF5FUNCTION_H_
- using namespace std;
-
- //HDF5写功能
- class Hdf5WriteValue
- {
- public:
- hid_t file, dataset, dataspace, group;
- herr_t status;
- hid_t dtype = H5Tcopy(H5T_C_S1);
-
- Hdf5WriteValue(){}; //构造函数
- ~Hdf5WriteValue(){};//析构函数
- void CreateNewFile();
- void CreateStringDataspace(const void *data, int rank, int col, int row);
- void CreateDataspace(int rank, int col, int row);
- void CreateGroup(string groupName);
- void CreateIntDataset(string datasetName);
- void CreateDoubleDataset(string datasetName);
- void CreateStringDataset(string datasetName);
- void WriteIntValue(const void *data);
- void WriteDoubleValue(const void *data);
- void WriteStringValue(const void *data);
- void CloseFile();
- };
-
- class Hdf5ReadValue
- {
- public:
- hid_t file, dataset, dataspace, group;
- herr_t status;
- hid_t dtype = H5Tcopy(H5T_C_S1);
-
- Hdf5ReadValue(){}; //构造函数
- ~Hdf5ReadValue(){};//析构函数
- void OpenFile(const char* filepath);
- void OpenGroup(const char* group);
- void OpenDataset(const char* dataset);
- void ReadIntData(void *data);
- void ReadDoubleData(void *data);
- void ReadStringData(void *data);
- void CloseFile();
- };
- #endif
Tools.cpp
- #include
- #include
- #include
-
- using namespace std;
-
- string getLocalTime(string format)
- {
- char char_time[64];
- time_t curr_time = time(NULL);
- tm* time_info = std::localtime(&curr_time);
- if (NULL != time_info)
- {
- strftime(char_time, sizeof(char_time), format.c_str(), time_info);
- }
- else
- {
- strcpy(char_time, "NULL");
- }
- return char_time;
- }
- string hdf5Filename()
- {
- string filename_id = "hdf5.out.";
- string time_stamp = getLocalTime("%Y.%m.%d.%H.%M.%S");
- string filename_m = filename_id + time_stamp + ".h5";
- return filename_m;
- }
Tools.h
- #include
- #include
- #include
- #include "hdf5.h"
-
- #ifndef TOOLS_H_
- #define TOOLS_H_
-
- using namespace std;
-
- string getLocalTime(string);
- string hdf5Filename();
-
- #endif
- #include
- #include
- #include "Hdf5Function.h"
-
- int main()
- {
- /*测试代码*/
- //一维int
- const int a = 99;
- const int*value1 = &a;
- //一维double
- const double b = 99.99;
- const double* value2 = &b;
- //一维char
- const char* value3 = "abc";
- //二维intle数组
- int value4[5][6];
- int i, j;
- for (i = 0; i < 5; i++)
- {
- for (j = 0; j < 6; j++)
- {
- value4[i][j] = i + j;
- }
- }
- //二维double数组
- double value5[289][28];
- int i, j;
- for (i = 0; i < 289; i++)
- {
- for (j = 0; j < 28; j++)
- {
- value5[i][j] = 0.1 + i + j;
- }
- }
-
- //存放读数据的数据结构
- double* result = new double[289 * 28];
- char* result1 = new char[20];
-
- //同一个文件内写数据
- Hdf5WriteValue write;
- write.CreateNewFile();
- //写一个int数据
- write.CreateDataspace(1, 0, 1);
- write.CreateGroup("groupA");
- write.CreateIntDataset("datasetA");
- write.WriteIntValue(value1);
- //写一个double数据
- write.CreateDoubleDataset("datasetB");
- write.WriteDoubleValue(value2);
- //新建一个group写一个string数据
- write.CreateGroup("groupB");
- write.CreateStringDataspace(value3,1, 0, 1);
- write.CreateStringDataset("datasetC");
- write.WriteStringValue(value3);
- //写一个int二维数组
- write.CreateDataspace(2, 5, 6);
- write.CreateIntDataset("datasetD");
- write.WriteIntValue(value4);
- //写一个double二维数组
- write.CreateDataspace(2, 289, 28);
- write.CreateDoubleDataset("datasetE");
- write.WriteDoubleValue(value5);
-
- write.CloseFile();
-
- //读H5或HDF5文件
- Hdf5ReadValue read;
- //读double数据
- char filename[] = "hdf5.h5";
- read.OpenFile(filename);
- read.OpenGroup("groupB");
- read.OpenDataset("datasetE");
- read.ReadDoubleData(result);
- //打印测试
- cout.setf(ios::fixed, ios::floatfield);
- cout.precision(2);
- for (i = 0; i < 8092; i++)
- {
- cout << result[i] << endl;
- }
-
- //读取字符串数据
- read.OpenGroup("groupB");
- read.OpenDataset("datasetC");
- read.ReadStringData(result1);
- cout << result1 << endl;
-
- read.CloseFile();
- delete result;
- delete result1;
- system("pause");
- return 0;
-
- }