我在Windows平台上使用HDF5 CPP库。基本上我想写一个复合数据类型到H5文件,其中包含std :: string。
此代码没有给出任何错误,但在写入H5文件时,它会写入垃圾值......
任何暗示都有帮助...
这是我的代码: -
#include <H5Cpp.h >
#include <vector>
#include <string>
#include <iostream>
using namespace std;
using namespace H5;
/**Compound datatype with STL Datatype*/
struct PostProcessingData
{
std::string datatype;
};
void main()
{
H5File file("ResultData.h5",H5F_ACC_TRUNC);
Group grp(file.createGroup("PostProcessing"));
PostProcessingData data;
data.datatype = "stress:xx";
// create spatial structure of data
hsize_t len = 1;
hsize_t dim[1] = {len};
int rank = 1;
DataSpace space(rank,dim);
// write required size char array
hid_t strtype = H5Tcopy (H5T_C_S1);
H5Tset_size (strtype, H5T_VARIABLE);
//defining the datatype to pass HDF55
H5::CompType mtype(sizeof(PostProcessingData));
mtype.insertMember("Filename", HOFFSET(PostProcessingData, datatype), strtype);
DataSet dataset = grp.createDataSet("subc_id2",mtype,space);
dataset.write(&data,mtype);
space.close();
mtype.close();
dataset.close();
grp.close();
file.close();
exit(0);
}
答案 0 :(得分:1)
我有以下解决方法在HDF5中存储字符串: - 我必须将std :: string转换为char *指针。 HDF5非常适合原始数据类型。
#include "H5Cpp.h"
#include <vector>
#include <string>
#include <iostream>
using namespace std;
using namespace H5;
/**Compound datatype with STL Datatype*/
struct PostProcessingData
{
char* datatype;
};
void main()
{
H5File file("ResultData.h5",H5F_ACC_TRUNC);
Group grp(file.createGroup("PostProcessing"));
PostProcessingData data;
std::string dt = "stress:xx";
data.datatype = new char[dt.length()];
data.datatype = const_cast<char*> (dt.data());
// create spatial structure of data
hsize_t len = 1;
hsize_t dim[1] = {len};
int rank = 1;
DataSpace space(rank,dim);
// write required size char array
hid_t strtype = H5Tcopy (H5T_C_S1);
H5Tset_size (strtype, H5T_VARIABLE);
//defining the datatype to pass HDF55
H5::CompType mtype(sizeof(PostProcessingData));
mtype.insertMember("Filename", HOFFSET(PostProcessingData, datatype), strtype);
DataSet dataset = grp.createDataSet("subc_id2",mtype,space);
//While writing data to file it gives following error
dataset.write(&data,mtype);
space.close();
mtype.close();
dataset.close();
grp.close();
file.close();
exit(0);
}