【问题标题】:Read string in HDF5/C++在 HDF5/C++ 中读取字符串
【发布时间】:2018-05-02 21:50:54
【问题描述】:

我在我的 HDF5 存档中存储了一个字符串(和一个向量),例如使用 Python 接口:

import h5py

file = h5py.File("example.h5","w")

file['/path/to/vector'] = [0., 1., 2.]
file['/path/to/string'] = 'test'

现在我想将字符串读取到std::string。我知道如何读取向量(见下文),但我完全不知道如何读取字符串。 特别不明白的是如何分配结果,如:

  • H5Cpp 库似乎不使用 STL 容器,而是使用原始指针,需要预先分配。
  • 这与观察 HDFView 指示维度大小为1 和类型为“字符串,长度 = 变量”有些矛盾。

这是我如何阅读矢量:

#include "H5Cpp.h"
#include <vector>
#include <iostream>

int main()
{
  // open file
  H5::H5File fid = H5::H5File("example.h5",H5F_ACC_RDONLY);

  // open dataset
  H5::DataSet   dataset    = fid.openDataSet("/path/to/vector");
  H5::DataSpace dataspace  = dataset.getSpace();
  H5T_class_t   type_class = dataset.getTypeClass();

  // check data type
  if ( type_class != H5T_FLOAT )
    throw std::runtime_error("Unable to read, incorrect data-type");

  // check precision
  // - get storage type
  H5::FloatType datatype = dataset.getFloatType();
  // - get number of bytes
  size_t precision = datatype.getSize();
  // - check precision
  if ( precision != sizeof(double) )
    throw std::runtime_error("Unable to read, incorrect precision");

  // get the size
  // - read rank (a.k.a number of dimensions)
  int rank = dataspace.getSimpleExtentNdims();
  // - allocate
  hsize_t hshape[rank];
  // - read
  dataspace.getSimpleExtentDims(hshape, NULL);
  // - total size
  size_t size = 0;
  for ( int i = 0 ; i < rank ; ++i ) size += static_cast<size_t>(hshape[i]);

  // allocate output
  std::vector<double> data(size);

  // read data
  dataset.read(const_cast<double*>(data.data()), H5::PredType::NATIVE_DOUBLE);

  // print data
  for ( auto &i : data )
    std::cout << i << std::endl;
}

(用h5c++ -std=c++14 so.cpp编译)

【问题讨论】:

    标签: python c++ hdf5


    【解决方案1】:

    我找到了解决办法:

    #include "H5Cpp.h"
    #include <vector>
    #include <iostream>
    
    int main()
    {
      // open file
      H5::H5File fid = H5::H5File("example.h5",H5F_ACC_RDONLY);
    
      // open dataset, get data-type
      H5::DataSet   dataset    = fid.openDataSet("/path/to/string");
      H5::DataSpace dataspace  = dataset.getSpace();
      H5::StrType   datatype   = dataset.getStrType();
    
      // allocate output
      std::string data;
    
      // read output
      dataset.read(data, datatype, dataspace);
    
      std::cout << data << std::endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 2015-09-29
      • 2017-09-29
      • 2019-01-29
      • 2013-01-03
      • 2014-06-11
      • 2014-11-21
      • 2011-11-01
      • 2020-08-13
      相关资源
      最近更新 更多