Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

same issue,Reading fixed length string from attribute #322 #613

Closed
wadv1232 opened this issue Oct 2, 2022 · 8 comments
Closed

same issue,Reading fixed length string from attribute #322 #613

wadv1232 opened this issue Oct 2, 2022 · 8 comments
Labels
String Anything related to handling strings, sequences of chars.

Comments

@wadv1232
Copy link

wadv1232 commented Oct 2, 2022

version:2.4.1
code:

HighFive::FixedLenStringArray<8> v;
file.getGroup("/what").getAttribute("date").read(v);

error:

inline bool checkDimensions(const DataSpace& mem_space, size_t input_dims) {

for (auto i = dims.crbegin(); i != --dims.crend() && *i == 1; ++i)

same issue :Reading fixed length string from attribute #322

@N-damo
Copy link

N-damo commented Nov 17, 2022

same issue

@N-damo
Copy link

N-damo commented Nov 18, 2022

dimension of the attribute is 0,so "Impossible to read DataSet of dimensions 0 into arrays of dimensions 1"?

@1uc
Copy link
Collaborator

1uc commented Nov 22, 2022

It sounds like you want to read a single fixed length string, not an array of fixed length strings. The class HighFive::FixedLenStringArray<8> models an array of strings, each of which has length 8.

In order to read a fixed length string you do something along the lines of:

    using fixed_len_str_t = std::array<char, 6>;
    // Create a new file using the default property lists.
    {
        File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
        auto attr = fixed_len_str_t{"Hello"};
        file.createAttribute("str", attr);
    }
    // Re-read it
    {
        File file(FILE_NAME);

        // without pre-allocation
        {
            auto attr = file.getAttribute("str").read<fixed_len_str_t>();
            CHECK(attr.data() == std::string("Hello"));
        }

        // with pre-allocation
        {
            auto attr = fixed_len_str_t{};
            file.getAttribute("str").read(attr);
            CHECK(attr.data() == std::string("Hello"));
        }
    }

Probably a char [6] would work too. Naturally, you could also read the string as a std::string.

@N-damo
Copy link

N-damo commented Nov 24, 2022

not work.
error:
Can't output std::string as fixed-length. Use raw arrays or FixedLenStringArray

image
image

@kailvn
Copy link

kailvn commented Nov 28, 2022

same issue, but the test case "HighFiveFixedLenStringArrayAttribute" work.

@1uc 1uc added the String Anything related to handling strings, sequences of chars. label Mar 17, 2023
@andrewliuhui
Copy link

@kailvn testcase has dimensions 1, so it can work.
If fixed-len-string attribute with dimension 0 , highfive cannot read that attribute.

@1uc
Copy link
Collaborator

1uc commented May 6, 2023

Thank you for this subtle difference.

We've been working towards improving std::string support. In particular, making it possible to read/write std::strings and containers (std::vector, std::array, etc) of std::string to/from any HDF5 string datatype.

This would hopefully eliminate the need for FixedLenStringArray entirely. The PR is #744.

@1uc
Copy link
Collaborator

1uc commented Oct 31, 2023

With #744 merged std::strings and containers thereof, e.g. std::vectorstd::string can be read and written to fixed or variable length HDF5 strings. There's an example here:
https://github.com/BlueBrain/HighFive/blob/master/src/examples/read_write_std_strings.cpp

@1uc 1uc closed this as completed Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
String Anything related to handling strings, sequences of chars.
Projects
None yet
Development

No branches or pull requests

5 participants