h5cpp icon indicating copy to clipboard operation
h5cpp copied to clipboard

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

Open FlyingSamson opened this issue 1 year ago • 2 comments

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).

FlyingSamson avatar Dec 09 '24 14:12 FlyingSamson