2014-02-11 73 views
1

我正在嘗試基本的hdf5數據集讀/寫操作在C++中。HDF5奇怪的例外

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

#ifndef H5_NO_NAMESPACE 
    using namespace H5; 
#endif 

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

int main() 
{ 
    try 
    { 
     int data[10]; 
     int dataOut[10]; 
     //Exception::dontPrint(); 

     std::cout<<"Enter The Data : "; 
     for(int i = 0 ; i < 10 ; i++) 
      std::cin>>data[i]; 

     H5File file(fName, H5F_ACC_TRUNC); 
     IntType type(H5T_NATIVE_INT); 

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

     hsize_t dim[] = {10}; 
     DataSpace dSpace(1,dim); 

     DataSet dSet = myGroup->createDataSet(dsName, type, dSpace); 
     dSet.write(data, type); 

     std::cout << "Data Written\n"; 
     dSet.read(dataOut, type); 

     std::cout<<"Data Read\n"; 
     for(int i = 0 ; i < 10 ; i ++) 
      std::cout<<dataOut[i]<<"\n"; 

     delete myGroup; 
    } 
    catch(Exception e) 
    { 
     e.printError(); 
    } 

    _getch(); 
    return 0; 
} 

輸入所有數據後,我得到異常:

HDF5-DIAG: Error detected in HDF5 (1.8.12) thread 0: 
#000: ..\..\src\H5F.c line 1503 in H5Fcreate(): unable to create file 
major: File accessibilty 
minor: Unable to open file 
#001: ..\..\src\H5F.c line 1285 in H5F_open(): unable to open file: time = Wed 
Feb 12 00:02:29 2014 
, name = '@╦>ÿK', tent_flags = 13 
major: File accessibilty 
minor: Unable to open file 
#002: ..\..\src\H5FD.c line 987 in H5FD_open(): open failed 
major: Virtual File Layer 
minor: Unable to initialize object 
#003: ..\..\src\H5FDsec2.c line 343 in H5FD_sec2_open(): unable to open file: 
name = '@╦>ÿK', errno = 22, error message = 'Invalid argument', flags = 13, o_fl 
ags = 302 
major: File accessibilty 
minor: Unable to open file 
HDF5-DIAG: Error detected in HDF5 (1.8.12) thread 0: 
#000: ..\..\src\H5F.c line 1503 in H5Fcreate(): unable to create file 
major: File accessibilty 
minor: Unable to open file 
#001: ..\..\src\H5F.c line 1285 in H5F_open(): unable to open file: time = Wed 
Feb 12 00:02:29 2014 
, name = '@╦>ÿK', tent_flags = 13 
major: File accessibilty 
minor: Unable to open file 
#002: ..\..\src\H5FD.c line 987 in H5FD_open(): open failed 
major: Virtual File Layer 
minor: Unable to initialize object 
#003: ..\..\src\H5FDsec2.c line 343 in H5FD_sec2_open(): unable to open file: 
name = '@╦>ÿK', errno = 22, error message = 'Invalid argument', flags = 13, o_fl 
ags = 302 
major: File accessibilty 
minor: Unable to open file 

但是,如果我硬編碼的文件名和數據集的名稱,如「abcd.h5」和「D設定」,然後,我能夠以獲得所需的輸出,但輸出後,我收到異常:

HDF5-DIAG: Error detected in HDF5 (1.8.12) thread 0: 
#000: ..\..\src\H5T.c line 1765 in H5Tclose(): immutable datatype 
major: Invalid arguments to routine 
minor: Bad value 
DataType::~DataType - H5Tclose failed 

請幫我弄清楚這個問題。

+0

只是一個樣式註釋:爲什麼你使用'myGroup'的指針?這是不必要的。 – Simon

回答

0

有兩個不同的問題。第一個是,不知何故,H5std_string實際上只是一個std::string在您的系統上受到損壞。看起來dset.h5被轉換成@╦>ÿK。我可能是錯的,但這就是它的外觀。爲此,我不知道這是Windows的問題,說實話,這有點可怕。

第二個問題來自type:析構函數抱怨它不能銷燬這個對象,因爲它是不可變的。那爲什麼它是不變的?由於使用的是this constructor

H5::IntType::IntType(const hid_t existing_id) 

這只是包裝一成不變H5T_NATIVE_INT型,而不是this one

H5::IntType::IntType(const PredType& pred_type) 

克隆H5T_NATIVE_INT,和克隆是可變的,更重要的是,可以被銷燬。所以,你需要更換:

IntType type(H5T_NATIVE_INT); 

通過

IntType type(PredType::NATIVE_INT); 

,你會好的。

+0

你可以完全擺脫'type',直接在你的'dSet.read'和'dSet.write'中使用'PredType :: NATIVE_INT'。 – Simon