c# hdf5 写string_将hdf5文件读取到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

#else

#include

#endif

#include

#include

#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

for(int j=0; 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

for(int j=0;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

for(uint j=0;j

cout << newbuffer[i][j] << " ";

} cout << endl;

}

cout << "Ok and now for the dynamic array:" << endl;

for(uint i=0; i

for(uint j=0;j

cout << p2DArray[i][j] << " ";

} cout << endl;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值