gpt4 book ai didi

c++ - HDF5 中的奇怪异常

转载 作者:行者123 更新时间:2023-11-30 04:08:39 32 4
gpt4 key购买 nike

我正在尝试使用 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

请帮我解决这个问题。

最佳答案

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

你会好起来的。

关于c++ - HDF5 中的奇怪异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21710328/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com