# Reading a string attribute in HDF5

**URL:** <https://forum.hdfgroup.org/t/reading-a-string-attribute-in-hdf5/3518>\
**Category:** Hdf-forum archives\
**Created:** [July 10, 2015, 8:22pm UTC](https://forum.hdfgroup.org/t/reading-a-string-attribute-in-hdf5/3518 "2015-07-10T20:22:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samer\_Afach](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@Samer\_Afach](https://forum.hdfgroup.org/u/Samer_Afach)\
**Post date:** [July 10, 2015, 8:22pm UTC](https://forum.hdfgroup.org/t/reading-a-string-attribute-in-hdf5/3518/1 "2015-07-10T20:22:07Z")

</div>

Dear all:

[Example.hdf5](https://hdf-discourse-1.s3.amazonaws.com/original/1X/78f32f5ac6781867190b0ad065e8d925ba916b8b.hdf5) (475 KB)

[main.cpp](https://hdf-discourse-1.s3.amazonaws.com/original/1X/b840245fc96ebb35cfe4937b999d2727826525f4.cpp) (2.11 KB)

> **···**
>
> 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.
> 
> ```
> The attribute is, as shown in HDF Viewer: Date = 2015\07\09
> 
> So "Date" is its name.
> 
> I'm trying to read the attribute, and it's not working, and I'm
> 
> ```
> 
> getting a messed set of characters. I created a self-contained  
> code and put it in the attachment together with the HDF5 file.
> 
> ```
> I tried changing the type to H5T_C_S1, but that throw an error and
> 
> ```
> 
> doesn’t work.
> 
> ```
> Could you please tell me why my code isn't reading the date
> 
> ```
> 
> attribute?
> 
> ```
> Thanks,
> 
> Samer Afach
> 
> ```

---

<div class="post-metadata">

**Author:** ![gheber](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/gheber/32/1919_2.png) [@gheber](https://forum.hdfgroup.org/u/gheber)\
**Post date:** [July 13, 2015, 4:34pm UTC](https://forum.hdfgroup.org/t/reading-a-string-attribute-in-hdf5/3518/2 "2015-07-13T16:34:18Z")

</div>

Samer, how are you? The problem is that the string in the file is a so-called ‘variable-length string.’  
(See [https://www.hdfgroup.org/HDF5/doc/UG/UG\_frame11Datatypes.html](https://www.hdfgroup.org/HDF5/doc/UG/UG_frame11Datatypes.html))  
From an API perspective, the main difference is that the library allocates the buffer  
for such string(s). You attempt (around line 52) to guestimate the string size is laudable but misguided.  
Instead of passing a pointer to H5Aread, you should pass a pointer to a pointer, i.e.,  
replace line 54 with just the definition “char\* date;” and pass “(void\*) &date” to H5Aread.  
Does that work for you?

G.

> **···**
>
> From: Hdf-forum [[mailto:hdf-forum-bounces@lists.hdfgroup.org](mailto:hdf-forum-bounces@lists.hdfgroup.org)] On Behalf Of Samer Afach  
> Sent: Friday, July 10, 2015 3:22 PM  
> To: [hdf-forum@lists.hdfgroup.org](mailto:hdf-forum@lists.hdfgroup.org)  
> Subject: [Hdf-forum] Reading a string attribute in HDF5
> 
> Dear all:
> 
> 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.
> 
> The attribute is, as shown in HDF Viewer: Date = 2015\07\09
> 
> So "Date" is its name.
> 
> I'm trying to read the attribute, and it's not working, and I'm getting a messed set of characters. I created a self-contained code and put it in the attachment together with the HDF5 file.
> 
> I tried changing the type to H5T\_C\_S1, but that throw an error and doesn't work.
> 
> Could you please tell me why my code isn't reading the date attribute?
> 
> Thanks,  
> Samer Afach
