gpt4 book ai didi

c++ - C/C++ HDF5 读取字符串属性

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:19 25 4
gpt4 key购买 nike

我的一位同事使用 labview 将 ASCII 字符串作为属性写入 HDF5 文件中。我可以看到该属性存在并读取它,但我无法打印它。

属性为,如HDF Viewer所示:

Date = 2015\07\09

所以“日期”是它的名字。

我正在尝试用这段代码读取属性

hsize_t sz = H5Aget_storage_size(dateAttribHandler);
std::cout<<sz<<std::endl; //prints 16
hid_t atype = H5Aget_type(dateAttribHandler);
std::cout<<atype<<std::endl; //prints 50331867
std::cout<<H5Aread(dateAttribHandler,atype,(void*)date)<<std::endl; //prints 0
std::cout<<date<<std::endl; //prints messy characters!
//even with an std::string
std::string s(date);
std::cout<<s<<std::endl; //also prints a mess

为什么会这样?如何将此字符串作为 const char*std::string 获取?

我也尝试过使用 atype = H5Tcopy (H5T_C_S1); 类型,但这也没有用......

编辑:在这里,我根据要求提供了一个完整的、独立的程序:

#include <string>
#include <iostream>
#include <fstream>
#include <hdf5/serial/hdf5.h>
#include <hdf5/serial/hdf5_hl.h>

std::size_t GetFileSize(const std::string &filename)
{
std::ifstream file(filename.c_str(), std::ios::binary | std::ios::ate);
return file.tellg();
}

int ReadBinFileToString(const std::string &filename, std::string &data)
{
std::fstream fileObject(filename.c_str(),std::ios::in | std::ios::binary);
if(!fileObject.good())
{
return 1;
}
size_t filesize = GetFileSize(filename);
data.resize(filesize);
fileObject.read(&data.front(),filesize);
fileObject.close();
return 0;
}

int main(int argc, char *argv[])
{
std::string filename("../Example.hdf5");
std::string fileData;
std::cout<<"Success read file into memory: "<<
ReadBinFileToString(filename.c_str(),fileData)<<std::endl;

hid_t handle;
hid_t magFieldsDSHandle;
hid_t dateAttribHandler;
htri_t dateAtribExists;

handle = H5LTopen_file_image((void*)fileData.c_str(),fileData.size(),H5LT_FILE_IMAGE_DONT_COPY | H5LT_FILE_IMAGE_DONT_RELEASE);
magFieldsDSHandle = H5Dopen(handle,"MagneticFields",H5P_DEFAULT);
dateAtribExists = H5Aexists(magFieldsDSHandle,"Date");
if(dateAtribExists)
{
dateAttribHandler = H5Aopen(magFieldsDSHandle,"Date",H5P_DEFAULT);
}


std::cout<<"Reading file done."<<std::endl;
std::cout<<"Open handler: "<<handle<<std::endl;
std::cout<<"DS handler: "<<magFieldsDSHandle<<std::endl;
std::cout<<"Attributes exists: "<<dateAtribExists<<std::endl;
hsize_t sz = H5Aget_storage_size(dateAttribHandler);
std::cout<<sz<<std::endl;
char* date = new char[sz+1];
std::cout<<"mem bef: "<<date<<std::endl;
hid_t atype = H5Aget_type(dateAttribHandler);
std::cout<<atype<<std::endl;
std::cout<<H5Aread(dateAttribHandler,atype,(void*)date)<<std::endl;
fprintf(stderr, "Attribute string read was '%s'\n", date);
date[sz] = '\0';
std::string s(date);
std::cout<<"mem aft: "<<date<<std::endl;
std::cout<<s<<std::endl;

H5Dclose(magFieldsDSHandle);
H5Fclose(handle);


return 0;
}

这个程序的打印输出:

Success read file into memory: 0
Reading file done.
Open handler: 16777216
DS handler: 83886080
Attributes exists: 1
16
mem bef:
50331867
0
Attribute string read was '�P7'
mem aft: �P7
�P7
Press <RETURN> to close this window...

谢谢。

最佳答案

事实证明,H5Aread 必须使用 char 指针的引用来调用...所以指针的指针:

H5Aread(dateAttribHandler,atype,&date);

请记住,不必为此保留内存。该库将保留内存,然后您可以使用 H5free_memory(date) 释放它。

这很好用。

编辑:

我了解到,只有当要读取的字符串具有可变长度时才会出现这种情况。如果字符串具有固定长度,则必须手动保留大小为 length+1 的内存,甚至手动将最后一个字符设置为 null(以获得以 null 结尾的字符串。有一个函数检查字符串长度是否固定的 hdf5 库。

关于c++ - C/C++ HDF5 读取字符串属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com