打开hdf5字符串数据集

时间:2013-12-03 18:16:27

标签: c++ hdf5

我只是想知道为什么我会收到这些错误。我已经包含了H5cpp.h.Here我只是想从现有的hdf5数据库中读取一个字符串。我也不确定如何定义memspace。谢谢

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

Reader.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;
}

2 个答案:

答案 0 :(得分:3)

这是一个关于如何使用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 

答案 1 :(得分:1)

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

  • 完全限定类型名称(即,将变量声明为H5::DataSetH5::DataSpaceH5::PredType
  • (如果使用C ++ 11)改为使用auto,让编译器弄清楚
  • using namespace H5放在文件顶部附近(如果您将其放在标题中,以#include标题的每个人都以隐式{{{ 1}}他们是否想要;但在.cpp文件中进行using namespace H5声明是可以的。)