• 封装了一个简单的C++ HDF5工具库,实现常用数据类型的读写


    目录

    一.开发环境

    二.主要功能

    三.文件结构

    四.HDF5写数据到hdf5文件功能实现

    五.HDF5读hdf5文件数据到程序中数据结构功能实现

    六.头文件Hdf5Function.h

    七.工具类

    八.主函数测试读写功能


    本文基于HDF5官方库,封装了一个简单的常用数据类型的库,HDF5支持的数据类型很多,其中的功能也很强大,但是日常开发中可能常用的包括int、double、string等数据,所以为了简化官方库的使用,自己弄了个小玩意,水平不足,还请见谅。

    一.开发环境

    Visual Studio2013+配置HDF5环境,也可以直接源码和HDF5库CMake编译。

    如何配置HDF5开发环境请参考我的另一篇文章

    如何配置HDF5环境

    二.主要功能

    1.HDF5写int,double,string类型的数据到hdf5文件中;

    2.读hdf5文件中int,double,string的数据到程序的数据结构中;

    3.写数据生成的hdf5文件名使用当前的时间戳来命名。

    三.文件结构

    • Hdf5Function.h
    • Tools.h
    • Hdf5WriteValue.cpp
    • Hdf5ReadValue.cpp
    • Tools.cpp
    • 测试用的主函数main.cpp

    四.HDF5写数据到hdf5文件功能实现

    创建了类来实现相关功能,完整代码见:

    HDF5-ZhaoDaBaoZzz-github.com

    1. #include "Tool.h"
    2. #include "Hdf5Function.h"
    3. using namespace std;
    4. Hdf5WriteValue::Hdf5WriteValue(){}; //构造函数
    5. Hdf5WriteValue::~Hdf5WriteValue(){};//析构函数
    6. /*创建HDF5*/
    7. void Hdf5WriteValue::CreateNewFile()
    8. {
    9. this->file=H5Fcreate(hdf5_filename().c_str,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);
    10. //H5F_ACC_TRUNC能覆盖,H5F_ACC_EXCL不能覆盖
    11. }
    12. /*创建HDF5 String数据维度*/
    13. void Hdf5WriteValue::CreateStringDataspace(const void *data,int rank,int col,int row)
    14. {
    15. hsize_t dim[2];
    16. dim[0]=row;
    17. dim[1]=col;
    18. size_t size=sizeof(data)/sizeof(char);
    19. this->status=H5Test_size(dtype,size)
    20. this->dataspace=H5Screate_simple(rank,dim,NULL);
    21. }
    22. /*创建HDF5数据维度*/
    23. void Hdf5WriteValue::CreateDataspace(int rank,int col,int row)
    24. {
    25. hsize_t dim[2];
    26. dim[0]=row;
    27. dim[1]=col;
    28. this->dataspace=H5Screate_simple(rank,dim,NULL);
    29. }
    30. /*创建group*/
    31. void Hdf5WriteValue::CreateGroup(string groupName)
    32. {
    33. this->group=H5Gcreate(this->file,groupName.c_str(),H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
    34. }
    35. /*创建dataset,int,double,string*/
    36. void Hdf5WriteValue::CreateIntDataset(string datasetName)
    37. {
    38. this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),H5T_NATIVE_INT,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
    39. }
    40. void Hdf5WriteValue::CreateDoubleDataset(string datasetName)
    41. {
    42. this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),H5T_NATIVE_Double,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
    43. }
    44. void Hdf5WriteValue::CreateStringDataset(string datasetName)
    45. {
    46. this->dataset=H5Dcreate2(this->group,datasetNmae.c_str(),this->dtype,this->dataspace,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT)
    47. }
    48. /*写入数据到对应类型的dataset*/
    49. void Hdf5WriteValue::WriteIntValue(const void *data)
    50. {
    51. this->status=H5Dwrite(this->dataset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
    52. }
    53. void Hdf5WriteValue::WriteDoubleValue(const void *data)
    54. {
    55. this->status=H5Dwrite(this->dataset,H5T_NATIVE_DOUNLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
    56. }
    57. void Hdf5WriteValue::WriteStringValue(const void *data)
    58. {
    59. this->status=H5Dwrite(this->dataset,this->dtype,H5S_ALL,H5S_ALL,H5P_DEFAULT,data);
    60. }
    61. /*关闭数据及文件,注意关闭顺序*/
    62. void Hdf5WriteValue::CloseFile()
    63. {
    64. this->status=H5Dclose(this->dataset);
    65. this->status=H5Sclose(this->dataspace);
    66. this->status=H5Gclose(this->group);
    67. this->status=H5Fclose(this->file);
    68. }

    五.HDF5读hdf5文件数据到程序中数据结构功能实现

    1. #include "Tools.h"
    2. #include "Hdf5Function.h"
    3. using namespace std;
    4. Hdf5ReadValue::Hdf5ReadValue(){}; //构造函数
    5. Hdf5ReadValue::~Hdf5ReadValue(){};//析构函数
    6. /*打开HDF5文件*/
    7. void Hdf5ReadValue::OpenFile(const char* filepath)
    8. {
    9. this->file = H5Fopen(filepath, H5F_ACC_RDONLY, H5P_DEFAULT);
    10. }
    11. /*打开HDF5文件对应的Group*/
    12. void Hdf5ReadValue::OpenGroup(const char* group)
    13. {
    14. this->group = H5Gopen2(this->file, group, H5P_DEFAULT);
    15. }
    16. /*打开HDF5文件对应的Dataset*/
    17. void Hdf5ReadValue::OpenDataset(const char* dataset)
    18. {
    19. this->dataset = H5Dopen(this->group, dataset, H5P_DEFAULT);
    20. }
    21. /*读文件中的各种类型数据int,double,string*/
    22. void Hdf5ReadValue::ReadIntData(void *data)
    23. {
    24. this->status = H5Dread(this->OpenDataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    25. }
    26. void Hdf5ReadValue::ReadDoubleData(void *data)
    27. {
    28. this->status = H5Dread(this->OpenDataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    29. }
    30. void Hdf5ReadValue::ReadStringData(void *data)
    31. {
    32. size_t size = sizeof(data) / sizeof(char);
    33. this->status = H5Test_size(dtype, size);
    34. this->status = H5Dread(this->OpenDataset, this->dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    35. }
    36. /*关闭数据及文件*/
    37. void Hdf5ReadValue::CloseFile()
    38. {
    39. this->status = H5Dclose(this->dataset);
    40. this->status = H5Gclose(this->group);
    41. this->status = H5Fclose(this->file);
    42. }

    六.头文件Hdf5Function.h

    1. #include
    2. #include
    3. #include
    4. #include "hdf5.h"
    5. #ifndef HDF5FUNCTION_H_
    6. #define HDF5FUNCTION_H_
    7. using namespace std;
    8. //HDF5写功能
    9. class Hdf5WriteValue
    10. {
    11. public:
    12. hid_t file, dataset, dataspace, group;
    13. herr_t status;
    14. hid_t dtype = H5Tcopy(H5T_C_S1);
    15. Hdf5WriteValue(){}; //构造函数
    16. ~Hdf5WriteValue(){};//析构函数
    17. void CreateNewFile();
    18. void CreateStringDataspace(const void *data, int rank, int col, int row);
    19. void CreateDataspace(int rank, int col, int row);
    20. void CreateGroup(string groupName);
    21. void CreateIntDataset(string datasetName);
    22. void CreateDoubleDataset(string datasetName);
    23. void CreateStringDataset(string datasetName);
    24. void WriteIntValue(const void *data);
    25. void WriteDoubleValue(const void *data);
    26. void WriteStringValue(const void *data);
    27. void CloseFile();
    28. };
    29. class Hdf5ReadValue
    30. {
    31. public:
    32. hid_t file, dataset, dataspace, group;
    33. herr_t status;
    34. hid_t dtype = H5Tcopy(H5T_C_S1);
    35. Hdf5ReadValue(){}; //构造函数
    36. ~Hdf5ReadValue(){};//析构函数
    37. void OpenFile(const char* filepath);
    38. void OpenGroup(const char* group);
    39. void OpenDataset(const char* dataset);
    40. void ReadIntData(void *data);
    41. void ReadDoubleData(void *data);
    42. void ReadStringData(void *data);
    43. void CloseFile();
    44. };
    45. #endif

    七.工具类

    Tools.cpp

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. string getLocalTime(string format)
    6. {
    7. char char_time[64];
    8. time_t curr_time = time(NULL);
    9. tm* time_info = std::localtime(&curr_time);
    10. if (NULL != time_info)
    11. {
    12. strftime(char_time, sizeof(char_time), format.c_str(), time_info);
    13. }
    14. else
    15. {
    16. strcpy(char_time, "NULL");
    17. }
    18. return char_time;
    19. }
    20. string hdf5Filename()
    21. {
    22. string filename_id = "hdf5.out.";
    23. string time_stamp = getLocalTime("%Y.%m.%d.%H.%M.%S");
    24. string filename_m = filename_id + time_stamp + ".h5";
    25. return filename_m;
    26. }

    Tools.h

    1. #include
    2. #include
    3. #include
    4. #include "hdf5.h"
    5. #ifndef TOOLS_H_
    6. #define TOOLS_H_
    7. using namespace std;
    8. string getLocalTime(string);
    9. string hdf5Filename();
    10. #endif

    八.主函数测试读写功能

    1. #include
    2. #include
    3. #include "Hdf5Function.h"
    4. int main()
    5. {
    6. /*测试代码*/
    7. //一维int
    8. const int a = 99;
    9. const int*value1 = &a;
    10. //一维double
    11. const double b = 99.99;
    12. const double* value2 = &b;
    13. //一维char
    14. const char* value3 = "abc";
    15. //二维intle数组
    16. int value4[5][6];
    17. int i, j;
    18. for (i = 0; i < 5; i++)
    19. {
    20. for (j = 0; j < 6; j++)
    21. {
    22. value4[i][j] = i + j;
    23. }
    24. }
    25. //二维double数组
    26. double value5[289][28];
    27. int i, j;
    28. for (i = 0; i < 289; i++)
    29. {
    30. for (j = 0; j < 28; j++)
    31. {
    32. value5[i][j] = 0.1 + i + j;
    33. }
    34. }
    35. //存放读数据的数据结构
    36. double* result = new double[289 * 28];
    37. char* result1 = new char[20];
    38. //同一个文件内写数据
    39. Hdf5WriteValue write;
    40. write.CreateNewFile();
    41. //写一个int数据
    42. write.CreateDataspace(1, 0, 1);
    43. write.CreateGroup("groupA");
    44. write.CreateIntDataset("datasetA");
    45. write.WriteIntValue(value1);
    46. //写一个double数据
    47. write.CreateDoubleDataset("datasetB");
    48. write.WriteDoubleValue(value2);
    49. //新建一个group写一个string数据
    50. write.CreateGroup("groupB");
    51. write.CreateStringDataspace(value3,1, 0, 1);
    52. write.CreateStringDataset("datasetC");
    53. write.WriteStringValue(value3);
    54. //写一个int二维数组
    55. write.CreateDataspace(2, 5, 6);
    56. write.CreateIntDataset("datasetD");
    57. write.WriteIntValue(value4);
    58. //写一个double二维数组
    59. write.CreateDataspace(2, 289, 28);
    60. write.CreateDoubleDataset("datasetE");
    61. write.WriteDoubleValue(value5);
    62. write.CloseFile();
    63. //读H5或HDF5文件
    64. Hdf5ReadValue read;
    65. //读double数据
    66. char filename[] = "hdf5.h5";
    67. read.OpenFile(filename);
    68. read.OpenGroup("groupB");
    69. read.OpenDataset("datasetE");
    70. read.ReadDoubleData(result);
    71. //打印测试
    72. cout.setf(ios::fixed, ios::floatfield);
    73. cout.precision(2);
    74. for (i = 0; i < 8092; i++)
    75. {
    76. cout << result[i] << endl;
    77. }
    78. //读取字符串数据
    79. read.OpenGroup("groupB");
    80. read.OpenDataset("datasetC");
    81. read.ReadStringData(result1);
    82. cout << result1 << endl;
    83. read.CloseFile();
    84. delete result;
    85. delete result1;
    86. system("pause");
    87. return 0;
    88. }

  • 相关阅读:
    基于python命令流及代码的Plaxis自动化建模
    jvm性能调优实战 - 61常用的JVM调优网站
    nodejs事件循环
    230902-部署Gradio到已有FastAPI及服务器中
    嵌入式UI界面开发就是这么简单
    谷粒学院16万字笔记+1600张配图(十四)——注册、登录
    01 - Android系统架构
    1045 快速排序
    【C++】类与对象基本知识 (构造 析构 拷贝 explicit 对象数组 动态静态对象)
    论文推荐:当自监督遇到主动学习
  • 原文地址:https://blog.csdn.net/qq_35902025/article/details/127686086