HDF5中的奇怪例外

时间:2014-02-11 18:40:29

标签: c++ 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”和“dSet”,那么,我能够得到所需的输出,但在输出后,我得到例外:

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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

有两个截然不同的问题。第一个是,不知何故,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);

你会很好。