打开hdf5字符串数据集

Open hdf5 string dataset

本文关键字:数据集 字符串 hdf5 打开      更新时间:2023-10-16

我只是想知道为什么会出现这些错误。我已经包含了H5cpp.h。这里我只是想从现有的hdf5数据库中读取一个字符串。此外,我也不确定如何定义内存空间。谢谢

Reader.cpp: In member function ‘void Reader::SetFasta()’:
Reader.cpp:10: error: ‘DataSet’ was not declared in this scope
Reader.cpp:10: error: expected ‘;’ before ‘dataset’
Reader.cpp:11: error: ‘DataSpace’ was not declared in this scope
Reader.cpp:11: error: expected ‘;’ before ‘dataspace’
Reader.cpp:12: error: ‘dataset’ was not declared in this scope
Reader.cpp:12: error: ‘PredType’ has not been declared
Reader.cpp:12: error: expected unqualified-id before ‘(’ token
Reader.cpp:12: error: ‘memspace’ was not declared in this scope
Reader.cpp:12: error: ‘dataspace’ was not declared in this scope

读卡器.h

#ifndef READER_H
#define READER_H
#include <string>
#include "H5Cpp.h"
#define FILEHDF "/media/data/back_up.h5"
class Reader
{
 private:
 hid_t file_id, dataset_id, dataspace_id, group_id,strtype, memtype;
 hsize_t dims[1];
 herr_t status; 
 std::string m_fasta;
 Reader() {}
 public:
 Reader(std::string prot_name);
 void SetFasta();
 std::string GetFasta() {return m_fasta;}
};
#endif

Reader.cpp

#include "Reader.h"
Reader::Reader(std::string prot_name)
{
 file_id=H5Fopen(FILEHDF, H5F_ACC_RDWR, H5P_DEFAULT);
 group_id=H5Gopen2(file_id, prot_name.c_str(), H5P_DEFAULT);
 SetFasta();
}
void Reader::SetFasta()
{
 DataSet dataset=file_id.openDataSet("Fasta_seq");
 DataSpace dataspace=dataset.getSpace();
 dataset.read(m_fasta, PredType::H5T_C_S1, memspace, dataspace);
}

main.cpp

#include <iostream>
using namespace std;
#include <string>
#include "Reader.h"
#include "H5Cpp.h"
int main()
{ std::string prot_name, fasta_seq;
 prot_name="102LA";
 Reader rd(prot_name);
 fasta_seq=rd.GetFasta();
 cout<<fasta_seq;
 return 0;
}

这是一个关于如何从C++使用HDF5的(非常简单)示例:

#include<iostream>
#include<array>
#include<H5Cpp.h>
int main() {
  // store this array as a 3x2 matrix:
  //
  // | 1 2 |
  // | 3 4 |
  // | 5 6 |
  //
  std::array<int, 6> data = {1, 2, 3, 4, 5, 6};

  H5::H5File fp("../data/test01.h5", H5F_ACC_TRUNC);
  hsize_t dim[2] = {3, 2};
  H5::DataSpace dspace(2, dim); // 2 is the rank of the matrix
  H5::DataSet dset = fp.createDataSet("My Test01 Data", H5::PredType::NATIVE_DOUBLE, dspace);
  dset.write(data.data(), H5::PredType::NATIVE_INT);
  return 0;
}

这个例子有点复杂,但展示了如何进行读写操作(非常简单):

#include<iostream>
#include<vector>
#include<H5Cpp.h>
void store(const std::string& filename, const std::string& dataset, const std::vector<double>& data) {
  H5::H5File fp(filename.c_str(), H5F_ACC_TRUNC);
  hsize_t dim[2] = {data.size(), 1};
  H5::DataSpace dspace(1, dim); // 1 is the rank of the matrix
  H5::DataSet dset = fp.createDataSet(dataset.c_str(), H5::PredType::NATIVE_DOUBLE, dspace);
  dset.write(data.data(), H5::PredType::NATIVE_DOUBLE);
  fp.close();  
}
std::vector<double> load(const std::string& filename, const std::string& dataset) {
  H5::H5File fp(filename.c_str(), H5F_ACC_RDONLY);
  H5::DataSet dset = fp.openDataSet(dataset.c_str());
  H5::DataSpace dspace = dset.getSpace();
  hsize_t rank;
  hsize_t dims[2];  
  rank = dspace.getSimpleExtentDims(dims, nullptr);
  std::vector<double> data;
  data.resize(dims[0]);
  dset.read(data.data(), H5::PredType::NATIVE_DOUBLE, dspace);
  fp.close();
  return data;
}
int main() {
  std::vector<double> data = {1, 2, 3, 4, 5, 6};
  store("mydata.h5", "my dataset", data);
  auto data_read = load("mydata.h5", "my dataset");
  for(auto item: data_read) {
    std::cout<<item<<" ";
  }
  std::cout<<std::endl;
  return 0;
}

我在OS X 10.7.4中使用GCC 4.8.1编译:

$ g++ example.cpp -std=c++11 -lhdf5_cpp -lhdf5

并获得:

$ ./a.out
1 2 3 4 5 6 

看起来DataSetDataSpacePredType都在H5命名空间中。您需要执行以下操作之一:

  • 完全限定类型名称(即,将变量声明为H5::DataSetH5::DataSpaceH5::PredType
  • (如果使用C++11)请改用auto,并让编译器计算出来
  • using namespace H5放在文件顶部附近(如果你将其放在一个标头中,无论是否需要,#include的标头中的每个人最终都会得到一个隐含的using namespace H5,这通常被认为是一种糟糕的形式;但在.cpp文件中有using声明也没问题)