title: C/C++ HDF5 Read string attribute
body: <p>A colleague of mine used labview to write an ASCII string as an attribute in an HDF5 file. I can see that the attribute exist, and read it, but I can't print it.</p>
  
  <p>The attribute is, as shown in HDF Viewer: </p>
  
  <blockquote>
    <p>Date = 2015\07\09</p>
  </blockquote>
  
  <p>So "Date" is its name.</p>
  
  <p>I'm trying to read the attribute with this code</p>
  
  <pre><code>hsize_t sz = H5Aget_storage_size(dateAttribHandler);
  std::cout&lt;&lt;sz&lt;&lt;std::endl; //prints 16
  hid_t atype = H5Aget_type(dateAttribHandler);
  std::cout&lt;&lt;atype&lt;&lt;std::endl; //prints 50331867
  std::cout&lt;&lt;H5Aread(dateAttribHandler,atype,(void*)date)&lt;&lt;std::endl; //prints 0
  std::cout&lt;&lt;date&lt;&lt;std::endl; //prints messy characters!
  //even with an std::string
  std::string s(date);
  std::cout&lt;&lt;s&lt;&lt;std::endl; //also prints a mess
  </code></pre>
  
  <p>Why is this happening? How can I get this string as a <code>const char*</code> or <code>std::string</code>?</p>
  
  <p>I tried also using the type <code>atype = H5Tcopy (H5T_C_S1);</code>, and that didn't work too...</p>
  
  <p><strong>EDIT:
  Here I provide a full, self-contained program as it was requested:</strong></p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;hdf5/serial/hdf5.h&gt;
  #include &lt;hdf5/serial/hdf5_hl.h&gt;
  
  std::size_t GetFileSize(const std::string &amp;filename)
  {
      std::ifstream file(filename.c_str(), std::ios::binary | std::ios::ate);
      return file.tellg();
  }
  
  int ReadBinFileToString(const std::string &amp;filename, std::string &amp;data)
  {
      std::fstream fileObject(filename.c_str(),std::ios::in | std::ios::binary);
      if(!fileObject.good())
      {
          return 1;
      }
      size_t filesize = GetFileSize(filename);
      data.resize(filesize);
      fileObject.read(&amp;data.front(),filesize);
      fileObject.close();
      return 0;
  }
  
  int main(int argc, char *argv[])
  {
      std::string filename("../Example.hdf5");
      std::string fileData;
      std::cout&lt;&lt;"Success read file into memory: "&lt;&lt;
                 ReadBinFileToString(filename.c_str(),fileData)&lt;&lt;std::endl;
  
      hid_t handle;
      hid_t magFieldsDSHandle;
      hid_t dateAttribHandler;
      htri_t dateAtribExists;
  
      handle = H5LTopen_file_image((void*)fileData.c_str(),fileData.size(),H5LT_FILE_IMAGE_DONT_COPY | H5LT_FILE_IMAGE_DONT_RELEASE);
      magFieldsDSHandle = H5Dopen(handle,"MagneticFields",H5P_DEFAULT);
      dateAtribExists = H5Aexists(magFieldsDSHandle,"Date");
      if(dateAtribExists)
      {
          dateAttribHandler = H5Aopen(magFieldsDSHandle,"Date",H5P_DEFAULT);
      }
  
  
      std::cout&lt;&lt;"Reading file done."&lt;&lt;std::endl;
      std::cout&lt;&lt;"Open handler: "&lt;&lt;handle&lt;&lt;std::endl;
      std::cout&lt;&lt;"DS handler: "&lt;&lt;magFieldsDSHandle&lt;&lt;std::endl;
      std::cout&lt;&lt;"Attributes exists: "&lt;&lt;dateAtribExists&lt;&lt;std::endl;
      hsize_t sz = H5Aget_storage_size(dateAttribHandler);
      std::cout&lt;&lt;sz&lt;&lt;std::endl;
      char* date = new char[sz+1];
      std::cout&lt;&lt;"mem bef: "&lt;&lt;date&lt;&lt;std::endl;
      hid_t atype = H5Aget_type(dateAttribHandler);
      std::cout&lt;&lt;atype&lt;&lt;std::endl;
      std::cout&lt;&lt;H5Aread(dateAttribHandler,atype,(void*)date)&lt;&lt;std::endl;
      fprintf(stderr, "Attribute string read was '%s'\n", date);
      date[sz] = '\0';
      std::string s(date);
      std::cout&lt;&lt;"mem aft: "&lt;&lt;date&lt;&lt;std::endl;
      std::cout&lt;&lt;s&lt;&lt;std::endl;
  
      H5Dclose(magFieldsDSHandle);
      H5Fclose(handle);
  
  
      return 0;
  }
  </code></pre>
  
  <p>Printed output of this program:</p>
  
  <pre><code>Success read file into memory: 0
  Reading file done.
  Open handler: 16777216
  DS handler: 83886080
  Attributes exists: 1
  16
  mem bef: 
  50331867
  0
  Attribute string read was '�P7'
  mem aft: �P7
  �P7
  Press &lt;RETURN&gt; to close this window...
  </code></pre>
  
  <p>Thanks.</p>