簡體   English   中英

HDF5將32位無符號int寫入磁盤上的64位,並讀入32位無符號int

[英]HDF5 write out 32 bit unsigned int to 64 bits on disk and read into 32 bit unsigned int

我正在嘗試使用HDF5在磁盤上寫出32位unsigned int到64位unsigned long的向量; 然后將其讀回內存中的32位unsigned int 為此,我的讀寫函數如下所示(我為32位unsigned int容器my_container實現了所有定義明確的push_backresizemy_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;
}

我把write有參數:常量,參考my_containerH5::Pred::NATIVE_UINT64 ,並read了以下參數:參考my_containerH5::Pred::NATIVE_UINT32 這可能是問題的根源之一。 讓我知道是否需要進一步澄清。 基本上,當我重新閱讀它時,我就會感到很垃圾。感謝那里的任何HDF5專家的建議。 謝謝你的時間。

解決方案在於更改寫入功能以接受文件和內存數據類型:

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;
}

然后其余的工作按預期進行-讀取功能基本上保持不變; 即使對於成對的無符號int等的容器也可以做到這一點。 HTH。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM