【发布时间】:2013-06-21 02:10:17
【问题描述】:
我正在尝试使用 HDF5 在磁盘上写出 32 位 unsigned int 到 64 位 unsigned long 的向量;然后将其读回内存中的 32 位 unsigned int。为此,我的读写函数如下所示(我为我的 32 位 unsigned int 容器、my_container 实现了 push_back、resize 等的所有明确定义的函数):
bool write(const my_container<unsigned int>& p, const std::string& datapath, const H5::DateType& datatype):
try {
const hsize_t h5size[1] = { p.size() };
const H5::DataSpace h5space(1, h5size);
const H5::DataSet h5set = fileptr_->createDataSet(datapath, datatype, h5space);
//-Tried using void* here for data, no difference
const unsigned int* data = &(*p.begin());
h5set.write(data, datatype);
}
catch( H5::Exception& ) {
//-Handle exception here
return false;
}
return true;
}
read(my_container<unsigned int>& p, const H5::DataType& datatype, const std::string& datapath) {
H5::DataType h5set_datatype = h5set.getDataType();
const std::size_t size = (std::size_t)h5space.getSimpleExtentNpoints();
try {
if(h5set_datatype == H5::PredType::NATIVE_UINT64 && datatype == H5::PredType::NATIVE_UINT32 ) {
typedef unsigned long long u64;
typedef std::vector<u64> u64vec;
u64vec ivector;
ivector.resize(size);
void* data = (void*)(&(*ivector.begin()));
h5set.read(data, h5set_datatype);
p.resize(0);
BOOST_FOREACH(const u64 &v, ivector) {
//-I've handled the cast below using numeric cast separately
p.push_back(v);
}
} //-End compare datatypes on disk and memory
} //-End try
catch(const H5::Exception &e) {
//-Handle exception
return false;
}
return true;
}
我用参数调用write:对my_container、H5::Pred::NATIVE_UINT64 和read 的常量引用:对my_container 和H5::Pred::NATIVE_UINT32 的引用。这可能是问题的根源之一。让我知道是否需要进一步澄清。基本上,当我读回它时,我得到了垃圾。感谢任何 HDF5 专家的建议。感谢您的宝贵时间。
【问题讨论】:
-
我想我可能已经解决了我的问题 - 需要做更多的测试来确认。
标签: c++ boost hdf5 unsigned-integer