HDF5文件操作中的运行时错误

问题描述:

我正在尝试一个程序,我将一个结构数组转换为字节数组,然后将它们多次保存到hdf5数据集。 (数据集的维数为100,所以不要做写操作100次)。在将结构转换为字节数组时,我没有任何问题,当我尝试选择需要在数据集中写入数据的hyperslab时,我似乎遇到了问题。我是hdf5的新手。请帮我解决这个问题。HDF5文件操作中的运行时错误

#include "stdafx.h" 
#include "h5cpp.h" 
#include <iostream> 
#include <conio.h> 
#include <string> 

#ifndef H5_NO_NAMESPACE 
    using namespace H5; 
#endif 

using std::cout; 
using std::cin; 
using std::string; 

const H5std_string fName("dset.h5"); 
const H5std_string dsName("dset"); 

struct MyStruct 
{ 
    int x[1000],y[1000]; 
    double z[1000]; 
}; 


int main() 
{ 
    try 
    { 
     MyStruct obj[10]; 
     char* totalData; 
     char* inData; 
     hsize_t offset[1],count[1]; 

     H5File file("sample.h5", H5F_ACC_TRUNC); 

     StrType type(PredType::C_S1,100*sizeof(obj)); 

     Group *myGroup = new Group(file.createGroup("\\myGroup")); 

     hsize_t dim[] = {100}; 

     DataSpace dSpace(1,dim); 

     DataSet dSet = myGroup->createDataSet("dSet", type, dSpace); 

     for(int m = 0; m < 100 ; m++) 
     { 
      for(int j = 0 ; j < 10 ; j++) 
      { 
       for(int i = 0 ; i < 1000 ; i++) // some random values stored 
       { 
        obj[j].x[i] = i*13 + i*19; 
        obj[j].y[i] = i*37 - i*18; 
        obj[j].z[i] = (i + 1)/(0.4 * i); 
       } 
      } 
      totalData = new char[sizeof(obj)]; // converting struct to byte array 
      memcpy(totalData, &obj, sizeof(obj)); 

      cout<<"Start Write.\n"; 
      cout<<"Total Size : "<<sizeof(obj)/1000<<"KB\n"; 

      //Exception::dontPrint(); 

      hsize_t dim[] = { 1 }; //I think am screwing up between this line and following 5 lines 

      DataSpace memSpace(1, dim); 

      offset[0] = m; 
      count[0] = 1; 
      dSpace.selectHyperslab(H5S_SELECT_SET, count, offset); 

      dSet.write(totalData, type, memSpace, dSpace); 

      cout<<"Write Done.\n"; 
      cout<<"Read Start.\n"; 
      inData = new char[sizeof(obj)]; 
      dSet.read(inData, type); 
      cout<<"Read Done\n"; 
     } 
     delete myGroup; 
    } 
    catch(Exception e) 
    { 
     e.printError(); 
    } 
    _getch(); 
    return 0; 
} 

我得到的输出,

enter image description here

当我用它代替H5S_SELECT_SET H5S_SELECT_APPEND,输出称

Start Write. 
Total Size : 160KB 
HDF5-DIAG: Error detected in HDF5 (1.8.12) thread 0: 
#000: ..\..\src\H5Shyper.c line 6611 in H5Sselect_hyperslab(): unable to set hyperslab selection 
major: Dataspace 
minor: Unable to initialize object 
#001: ..\..\src\H5Shyper.c line 6477 in H5S_select_hyperslab(): invalid selection operation 
major: Invalid arguments to routine 
minor: Feature is unsupported 

请帮我这个情况。在此先感谢..

主要问题是您的type数据类型的大小。它应该是sizeof(obj)而不是100*sizeof(obj)

不管怎么说,你不应该使用字符串数据类型,但一个不透明数据类型,因为这是它是什么,这样你就可以代替这整个一行:

DataType type(H5T_OPAQUE, sizeof(obj)); 

第二个问题是在read。要么你阅读所有内容,并且需要确保inData足够大,即100*sizeof(obj)而不是sizeof(obj),或者您只需要选择要读取的元素,就像write一样。

+0

让我们假设我对阅读不感兴趣,并假设我排除了阅读部分,并继续向文件中写入数据。从你的回答中,我是否应该假设做出以下更改会使事情发挥作用? 1)将类型语句更改为DataType类型(H5T_OPAQUE,sizeof(obj)); (请记住我执行了100次写操作),请提一下这是对此程序执行写操作的正确方法。 – user3297129

+0

是的。请记住,您正在构建一个包含100个元素的数组,每个元素的大小为sizeof(obj)'。在HDF5中,数据类型是数组的_one_元素的类型,而_dataspaces_则包含元素的数量。 – Simon