Skip to content

Fixed-size string attributes include \0 in resulting std::string #661

@FlyingSamson

Description

@FlyingSamson

When I try to read a fixed-size string attribute with properties (from h5dump -e -a ...) like

ATTRIBUTE "Creator" {
   DATATYPE  H5T_STRING {
      STRSIZE 7;
      STRPAD H5T_STR_NULLTERM;
      CSET H5T_CSET_ASCII;
      CTYPE H5T_C_S1;
   }
   DATASPACE  SCALAR
   DATA {
   (0): "xxxxxx"
   }
}

using code like

std::string value;
attribute.read(value);

the resulting std::string value contains the terminating '\0' character as last character in its internal vector of characters.

I found the related issue #224, and am now unsure if this is intended behavior. I personally find this behavior rather odd and not really conforming to what I expected from an interface storing a string attribute into a std::string.

PS: Reading the same attribute but stored as non-fixed-size string in the hdf5 file

ATTRIBUTE "Creator" {
   DATATYPE  H5T_STRING {
      STRSIZE H5T_VARIABLE;
      STRPAD H5T_STR_NULLTERM;
      CSET H5T_CSET_ASCII;
      CTYPE H5T_C_S1;
   }
   DATASPACE  SCALAR
   DATA {
   (0): "xxxxxx"
   }
}

using the same c++ code, works as expected (i.e., no trailing \0 in the resulting std::string).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions