情况:
我尝试从.hdf5
文件中读取值。
系统:
我的代码:
//File Path
hid_t H5_hid_RESULTS = H5Fcreate (V_FIn_HDF5_Path.absoluteFilePath().toUtf8().constData(), H5F_ACC_RDONLY, H5P_DEFAULT, H5P_DEFAULT);
//Status (Error Output?)
herr_t status;
//read dataset "heigth"
int32_t heigth[1];
hid_t H5_hid_heigth = H5Dopen1(H5_hid_RESULTS, "heigth");
status = H5Dread(H5_hid_heigth, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, heigth);
qDebug() << "heigth" << heigth[0];
status = H5Dclose(H5_hid_heigth);
//Close: file
status = H5Fclose (H5_hid_RESULTS);
目标.hdf5文件(在查看器中):
结果:
qDebug打印一个随机数(例如:104610208)而不是预期的512。
问题:
我尝试了什么:
int[1]
而不是int32_t[1]
:相同的结果。int
而不是int32_t[1]
:D:\...\xyz.cpp:47: Error: C2664: "herr_t H5Dread(hid_t,hid_t,hid_t,hid_t,hid_t,void *)" : Conversion from argument 6 from "int" to "void *" not possible
int*
而不是int32_t[1]
:0x7fe00000001
我猜这个价值的地址。如果我尝试打印*heigth
而不是heigth
,它会在调用函数时崩溃"/heigth"
而不是"heigth"
:相同的结果。正如文件查看器所示,您的数据类型512是H5T_NATIVE_INT32
而不是H5T_NATIVE_INT
。换句话说,您尝试读取的是64位整数,其中只有32位整数。这应该适合你:
status = H5Dread(H5_hid_heigth, H5T_NATIVE_INT32,
H5S_ALL, H5S_ALL, H5P_DEFAULT, heigth);
我用这个example在这里激发了一个完全不同但非常简单的方法解决了它。基本上与链接中的代码相同,但非常简化,因此新手到c ++和HDF5可以理解它:
//open file (My path is zensored, project from work)
const H5std_string H5_Path_Results("C:/.../results.hdf5");
H5File H5_File_Results(H5_Path_Results, H5F_ACC_RDONLY);
//open set
const H5std_string H5_Nam_Height("height");
DataSet H5_Set_Height = H5_File_Results.openDataSet(H5_Nam_Height);
//read set
int height[1];
H5_Set_Height.read(height, PredType::NATIVE_INT, H5S_ALL, H5S_ALL);
qDebug() << height[0];
//close set
H5_Set_Height.close();
//close file
H5_File_Results.close();
这给了我想要读取的512作为输出。
当然需要#include <hdf5.h>
和#include <H5Cpp.h>
,必须将库添加到项目中,并且必须存在.hdf5
文件。