# Read EnumType using C++ API

**URL:** <https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244>\
**Category:** C++ Users' Group\
**Created:** [October 9, 2019, 2:30pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244 "2019-10-09T14:30:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxwell130631](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@maxwell130631](https://forum.hdfgroup.org/u/maxwell130631)\
**Post date:** [October 9, 2019, 2:30pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/1 "2019-10-09T14:30:47Z")

</div>

Hi,

I would like to read in the possible values of an EnumType stored in a file. This is what I have so far:

> H5::H5File f( filename, H5F\_ACC\_RDONLY );  
> H5::Group g = f.openGroup(“/testGroup/analysis”);  
> H5::Attribute attribute = g.openAttribute(“type”);  
> H5::DataType type = attribute.getDataType();

The attribute in the file looks like this:  
ATTRIBUTE “type” {  
DATATYPE H5T\_ENUM {  
H5T\_STD\_I32LE;  
“bool” 3;  
“double” 1;  
“int” 0;  
“string” 2;  
“vector” 5;  
}  
DATASPACE SCALAR  
DATA {  
(0): analysis  
}  
}

How can I retrieve the pairs like “vector”,5 if I open a file with an attribute like above?

---

<div class="post-metadata">

**Author:** ![bmribler](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bmribler](https://forum.hdfgroup.org/u/bmribler)\
**Post date:** [October 9, 2019, 5:06pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/2 "2019-10-09T17:06:09Z")

</div>

Hello,

Would using H5::CompType be reasonable for each of those pairs?

---

<div class="post-metadata">

**Author:** ![maxwell130631](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@maxwell130631](https://forum.hdfgroup.org/u/maxwell130631)\
**Post date:** [October 9, 2019, 8:39pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/3 "2019-10-09T20:39:44Z")

</div>

I’m not sure I understand. I just want to make sure, that if I read the “type” attribute, my program understands what some value like 5 means, that it is a vector. This seems to be stored in the H5T\_ENUM, but I don’t know how I can access it. In the above example, I could do

> int value=-1;  
> attribute.read(type,&value);

do read the actual value of the EnumType. Still I would not know that 5 corresponds to vector. I found that there is the function EnumType::nameOf (void \*value, size\_t size) const. But in the above example, I would not know how to convert the DataType to an EnumType to use that function.

---

<div class="post-metadata">

**Author:** ![bmribler](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bmribler](https://forum.hdfgroup.org/u/bmribler)\
**Post date:** [October 10, 2019, 4:54am UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/4 "2019-10-10T04:54:15Z")

</div>

Ah, I see. Can you use H5::EnumType type = attribute.getEnumType(); instead of H5::DataType type = attribute.getDataType();?

---

<div class="post-metadata">

**Author:** ![mrkshdr](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/m/22d042/32.png) [@mrkshdr](https://forum.hdfgroup.org/u/mrkshdr)\
**Post date:** [October 10, 2019, 1:13pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/5 "2019-10-10T13:13:10Z")

</div>

Yes, this is exactly what I was looking for. Here the code fragment which lists all the members:

H5::H5File f( “/home/user/test.hdf5”, H5F\_ACC\_RDONLY );  
H5::Group g = f.openGroup("/testGroup/");  
H5::Attribute attribute = g.openAttribute(“type”);  
H5::EnumType type = attribute.getEnumType();  
int nMembers = type.getNmembers();  
for(int i=0;i\<nMembers;i++) {  
int v;  
type.getMemberValue(i,&v);  
string name = type.nameOf(&v,100);  
cout \<\< "HDF5 EnumType Members: " \<\< name \<\< " " \<\< v \<\< endl;  
}

---

<div class="post-metadata">

**Author:** ![mrkshdr](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/m/22d042/32.png) [@mrkshdr](https://forum.hdfgroup.org/u/mrkshdr)\
**Post date:** [October 10, 2019, 1:13pm UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/6 "2019-10-10T13:13:23Z")

</div>

I have a little additional question regarding memory consumption: Lets assume I have 10 attributes which have the same EnumType as type. Is then the whole EnumType (the whole map of strings and ints) stored for every attribute, or is the EnumType stored once per file, and an attribute only contains the value?

---

<div class="post-metadata">

**Author:** ![maddukuri2003](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@maddukuri2003](https://forum.hdfgroup.org/u/maddukuri2003)\
**Post date:** [June 6, 2020, 3:10am UTC](https://forum.hdfgroup.org/t/read-enumtype-using-c-api/6244/7 "2020-06-06T03:10:11Z")

</div>

what is 100 in string name = type.nameOf(&v,100);? why hard coded it?
