Reading a string from hdf5 in C++

后端 未结 3 1633
有刺的猬
有刺的猬 2021-01-06 23:40

I am reading in datasets from a H5 file in Microsoft Visual C++ 2008. Everything works fine for data of type int and double but I run into problems when I come across string

3条回答
  •  失恋的感觉
    2021-01-07 00:25

        auto dataset = file.openDataSet(kDatasetName);
        auto dataspace = dataset.getSpace();
        hsize_t dims_out[2];
        auto ndims = dataspace.getSimpleExtentDims(dims_out, nullptr);
        assert(ndims == 2);
        auto n = dims_out[0] * dims_out[1];
        auto data_type = dataset.getDataType();
        auto type_class = data_type.getClass();
        auto data_size = data_type.getSize();
        void* out = new char[n * data_size]();
        dataset.read(out, data_type);
        if (type_class == H5T_INTEGER) {
        } else if (type_class == H5T_STRING) {
          std::string* strs = new std::string[n];
          for (auto i = 0u; i < n; ++i) {
            auto len = data_size;
            auto c_str = out + data_size * i;
            for (auto p = c_str + len - 1; p != c_str && !*p; --p) --len;
            strs[i].assign(c_str, len);
          }
        }
        free(out);
    

    Checkout https://github.com/opentradesolutions/openalpha/blob/hdf5/src/openalpha/data.cc for full example

提交回复
热议问题