在c++中读取hdf5文件到动态数组
Reading hdf5 files to dynamic arrays in c++
由于堆栈的大小限制,我试图将大型3D hdf5文件读取到动态数组中。我已经尝试了几种不同的方法,并通过分割故障失败。下面是示例代码,以显示我的问题。我将非常感谢你的帮助!!
//This example was based on several examples which came in the c++ examples directory of the hdf5 package.
#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#include <new>
#include "hdf5.h"
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
const H5std_string FILE_NAME( "Test.h5" );
const H5std_string DATASET_NAME( "Identity" );
const int NX = 3; // dataset dimensions
const int NY = 3;
const int RANK = 2;
using namespace std;
int main (void)
{
int buffer[3][3];
// Make Identity Matrix for fill.
for(int i=0; i<NX; i++){
for(int j=0; j<NY; j++){
if(i==j) buffer[i][j] = 1;
else buffer[i][j]=0;
}
}
cout << "Lets check we filled it correctly: " << endl;
// First let's check we do have I matrix
for(int i=0; i<NX; i++){
for(int j=0;j<NY;j++){
cout << buffer[i][j] << " ";
} cout << endl;
}
// Now let's write into a file
H5File file( FILE_NAME, H5F_ACC_TRUNC );
hsize_t dimsf[2];
dimsf[0] = NX;
dimsf[1] = NY;
DataSpace dataspace( RANK, dimsf );
IntType datatype( PredType::NATIVE_INT );
datatype.setOrder( H5T_ORDER_LE );
DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
dataset.write( buffer, PredType::NATIVE_INT );
// Ok great, now let's try opening this array using both static and dynamic(new) type arrays:
H5File file1( FILE_NAME, H5F_ACC_RDONLY );
DataSet dataset1 = file1.openDataSet( DATASET_NAME );
/*
* Get filespace for rank and dimension
*/
DataSpace filespace = dataset1.getSpace();
/*
* Get number of dimensions in the file dataspace
*/
int rank = filespace.getSimpleExtentNdims();
/*
* Get and print the dimension sizes of the file dataspace
*/
hsize_t dims[2]; // dataset dimensions
rank = filespace.getSimpleExtentDims( dims );
/*
* Define the memory space to read dataset.
*/
DataSpace mspace1(RANK, dims);
int newbuffer[NX][NY]; //static array.
int **p2DArray;
p2DArray = new int*[NX];
for (uint i = 0; i < NX; ++i) {
p2DArray[i] = new int[NY];
}
dataset1.read( newbuffer, PredType::NATIVE_INT, mspace1, filespace );
dataset1.read( p2DArray, PredType::NATIVE_INT, mspace1, filespace );
// Lastly lets print the arrays to make sure we get the same thing:
cout << "Ok and now for the static array:" << endl;
for(uint i=0; i<NX; i++){
for(uint j=0;j<NY;j++){
cout << newbuffer[i][j] << " ";
} cout << endl;
}
cout << "Ok and now for the dynamic array:" << endl;
for(uint i=0; i<NX; i++){
for(uint j=0;j<NY;j++){
cout << p2DArray[i][j] << " ";
} cout << endl;
}
return 0;
}
您的p2DArray必须是int
的纯(1D)数组,而不是数组的数组。这是read
方法的期望。
分配足够的空间来存储NX*NY数字:
int *p2DArray = new int[NX*NY];
,然后使用以下公式访问数组:p2DArray[i][j] = p2DArray[i*NY + j].
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 输出没有重复元素的动态数组(收缩数组)C++
- 正在插入动态数组
- 如何在动态数组上使用搜索函数
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 静态数组的自由动态数组
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么 c++ 动态数组的大小没有改变?
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 指针的 C++ 动态数组 - 何时需要使用它?
- 删除动态数组时未定义标识符
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 动态数组的 C++ 重载加运算符
- 基于数组的列表 - 动态数组创建时出错