c++ - 在 C++ 中将 hdf5 文件读取到动态数组

标签 c++ hdf5

由于堆栈的大小限制,我正在尝试将大型 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]。

关于c++ - 在 C++ 中将 hdf5 文件读取到动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17110435/

相关文章:

c++ - 使用 MS VC++ 2008 构建的 HDF5 和使用 MS VC++ 2010 + boost 1.45.0 构建的应用程序中的 boost 1.39.0 库

c++ - 使用 Python 从 C++ 程序中获取输出(通过 cout)

C++:如何将此指针传递给此类?

c++ - 传递复杂类型时的常量正确性

c++ - 返回对模板类中私有(private)成员的引用

python - h5py读取时间在读取速度上有随机且剧烈的波动

c++ - OpenGL 图像中的格式转换

python - 在 Python 中检查 .h5 文件

hdf5 - dask 和并行 hdf5 写入

python - 从多个 panda 数据帧创建 HDF5