由字符编码H5std_string导致的H5FCreate: Unable to create file问题解决

首先感谢:http://blog.csdn.net/mrhiuser/article/details/69603826

在将代码在VS2013中编译后运行,报错:


VS中动态库连接技巧:http://blog.csdn.net/houwenbin1986/article/details/78854911

依赖库:hdf5.lib;hdf5_cpp.lib;

修改点:

将std::string转成char*

/*
* 来自:http://blog.csdn.net/mrhiuser/article/details/69603826
*/


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

#include "H5Cpp.h" //包含HDF5需要的头文件

#ifndef H5_NO_NAMESPACE
using namespace H5;
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif  // H5_NO_STD
#endif

const H5std_string FILE_NAME("h5_test.h5");//定义要创建的文件名字
const int RANK = 2; //定义数组的维度
const int M = 3;
const int N = 4;

//
// 注意,下面的H5std_string在多字节字符集时,需要使用.c_str()否则是乱码报错!!!
//
int main(void)
{
	const H5std_string GROUP_NAME("MyGroup");//定义要创建group的名字
	const H5std_string DATASET_NAME("MyData");//定义要创建dataset的名字
	const H5std_string  ATTR_NAME("MyAttribute");//定义要创建数据集属性的名字 

	//准备要存储的数据
	float *data = (float*)malloc(M*N*sizeof(float));
	float *tmp = NULL;
	for (int i = 0; i < M; ++i){
		for (int j = 0; j < N; ++j){
			tmp = data + i*N + j;
			*tmp = 1.3;
		}
	}
	try
	{
		// Turn off the auto-printing when failure occurs so that we can
		// handle the errors appropriately
		Exception::dontPrint();

		//创建文件(文件传入最好用char*,否则会乱码)
		H5File file(FILE_NAME.c_str(), H5F_ACC_TRUNC);

		//创建 group
		Group group(file.createGroup(GROUP_NAME.c_str()));

		//创建数据空间
		hsize_t dims[RANK];               // dataset dimensions
		dims[0] = M;
		dims[1] = N;
		DataSpace *dataspace = new DataSpace(RANK, dims);

		//创建数据集
		DataSet *dataset = new DataSet(group.createDataSet(DATASET_NAME.c_str(), PredType::NATIVE_FLOAT, *dataspace));

		//将准备好的数据,写到数据集中。
		dataset->write(data, PredType::NATIVE_FLOAT);

		// 创建数据集属性空间.
		int attr_data[2] = { 100, 200 };
		hsize_t attr_dims[1] = { 2 };
		DataSpace attr_dataspace = DataSpace(1, attr_dims);

		// 创建数据集的属性.
		Attribute attribute = dataset->createAttribute(ATTR_NAME.c_str(), PredType::STD_I32BE, attr_dataspace);

		// 写属性.
		attribute.write(PredType::NATIVE_INT, attr_data);

		// 关闭数据空间、数据集、group对象.
		delete dataspace;
		delete dataset;
		group.close();
	}

	// catch failure caused by the H5File operations
	catch (FileIException error)
	{
		error.printError();
		return -1;
	}
	// catch failure caused by the DataSpace operations
	catch (DataSpaceIException error)
	{
		error.printError();
		return -1;
	}
	// catch failure caused by the Group operations
	catch (GroupIException error)
	{
		error.printError();
		return -1;
	}
	// catch failure caused by the DataSet operations
	catch (DataSetIException error)
	{
		error.printError();
		return -1;
	}
	return 0;
}


运行脚本:

@echo off
set PATH=C:\Program Files\HDF_Group\HDF5\1.10.1\bin;%PATH%
echo "Create HDF5 file..."

x64\Debug\Project1.exe

pause

echo "Dump HDF5 file..."

h5dump.exe h5_test.h5

pause


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值