HDF5在磁盘上写出32位无符号整数到64位,并读入32位无符号整数

时间:2013-06-24 14:15:21

标签: c++ boost hdf5 unsigned-integer

我正在尝试使用HDF5在磁盘上写出32位unsigned int到64位unsigned long的向量;然后将其读回内存中的32位unsigned int。为此,我的读写函数如下所示(我已经为我的32位push_back容器{{1}实现了resizeunsigned int等所有明确定义的函数。 }}):

my_container

我使用参数调用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; } :使用参数对writemy_containerH5::Pred::NATIVE_UINT64进行const-reference:引用read和{{1} }。这可能是问题的根源之一。如果需要进一步澄清,请与我们联系。基本上我在读回来时会变得垃圾。感谢任何HDF5专家提出的建议。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

解决方案在于更改write函数以接受文件和内存数据类型:

bool write(const my_container<unsigned int>& p, const H5::DataType& file_datatype, const H5::DataType& mem_datatype, const std::string& datapath) const {

    try {
        const hsize_t h5size[1] = { p.size() };
        const H5::DataSpace h5space(1, h5size);

        const H5::DataSet h5set = fileptr_->createDataSet(datapath, file_datatype, h5space);
        const void* data = &(*p.begin());
        h5set.write(data, mem_datatype);
    } 
    catch( H5::Exception& ) {
        // Handle exception
        return false;
    }

    return true;
}

然后其余的按预期工作 - 读取功能基本不变;甚至对于无符号整数对的容器也能做同样的事情。 HTH。

相关问题