# HDF5 with C++

**URL:** <https://forum.hdfgroup.org/t/hdf5-with-c/1748>\
**Category:** Hdf-forum archives\
**Created:** [June 6, 2010, 3:47pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748 "2010-06-06T15:47:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/philipp_kraus/32/1654_2.png) [@Philipp\_Kraus](https://forum.hdfgroup.org/u/Philipp_Kraus)\
**Post date:** [June 6, 2010, 3:47pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/1 "2010-06-06T15:47:33Z")

</div>

Hi,

I try to implement HDF structures in C++ for using the datasets with the Boost libraries. Is there any good tutorial for using HDF with C++ class structures, because it's hard for me to translate C examples to C++. I found only the HDF API reference under [http://www.hdfgroup.org/HDF5/doc/cpplus\_RM/index.html](http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html)

At this time I had written code to read/write numeric vector and matrix structure. I have some trouble with arrays especially string types.

Does anyone know a How-To or anything else?

Thanks

Phil

---

<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:** [June 7, 2010, 1:30am UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/2 "2010-06-07T01:30:31Z")

</div>

Hi Phil,

Have you looked at  
[http://www.hdfgroup.org/HDF5/doc/cpplus\_RM/examples.html](http://www.hdfgroup.org/HDF5/doc/cpplus_RM/examples.html)?

Binh-Minh

> **···**
>
> -----Original Message-----  
> From: [hdf-forum-bounces@hdfgroup.org](mailto:hdf-forum-bounces@hdfgroup.org) [[mailto:hdf-forum-bounces@hdfgroup.org](mailto:hdf-forum-bounces@hdfgroup.org)]  
> On Behalf Of Kraus Philipp  
> Sent: Sunday, June 06, 2010 11:48 AM  
> To: [hdf-forum@hdfgroup.org](mailto:hdf-forum@hdfgroup.org)  
> Subject: [Hdf-forum] HDF5 with C++
> 
> Hi,
> 
> I try to implement HDF structures in C++ for using the datasets with  
> the Boost libraries. Is there any good tutorial for using HDF with C++  
> class structures, because it's hard for me to translate C examples to C  
> ++. I found only the HDF API reference under  
> [http://www.hdfgroup.org/HDF5/doc/cpplus\_RM/index.html](http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html)
> 
> At this time I had written code to read/write numeric vector and  
> matrix structure. I have some trouble with arrays especially string  
> types.
> 
> Does anyone know a How-To or anything else?
> 
> Thanks
> 
> Phil  
> &nbsp;&nbsp;  
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Hdf-forum is for HDF software users discussion.  
> [Hdf-forum@hdfgroup.org](mailto:Hdf-forum@hdfgroup.org)  
> [http://mail.hdfgroup.org/mailman/listinfo/hdf-forum\_hdfgroup.org](http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org)
> 
> --  
> Internal Virus Database is out-of-date.  
> Checked by AVG.  
> Version: 7.5.549 / Virus Database: 270.9.0/1778 - Release Date: 11/9/2008  
> 2:14 PM

---

<div class="post-metadata">

**Author:** ![Ger\_van\_Diepen](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/g/6de8d8/32.png) [@Ger\_van\_Diepen](https://forum.hdfgroup.org/u/Ger_van_Diepen)\
**Post date:** [June 7, 2010, 5:55am UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/3 "2010-06-07T05:55:10Z")

</div>

Hi Phil,

You can take a look at the classes in casa/HDF5 of casacore  
(casacore.google.com). It can read or write (hyperslabs of) N-dim arrays  
of various basic numeric types (including complex) in datasets. It can  
also read/write (small) arrays in attributes.  
The classes also deal with exception-safe handling of hid\_t variables.

Cheers,  
Ger

> > > Kraus Philipp \<philipp.kraus@flashpixx.de\> 6/6/2010 5:47 PM \>\>\>

Hi,

I try to implement HDF structures in C++ for using the datasets with  
the Boost libraries. Is there any good tutorial for using HDF with C++

class structures, because it's hard for me to translate C examples to  
C  
++. I found only the HDF API reference under  
[http://www.hdfgroup.org/HDF5/doc/cpplus\_RM/index.html](http://www.hdfgroup.org/HDF5/doc/cpplus_RM/index.html)

At this time I had written code to read/write numeric vector and  
matrix structure. I have some trouble with arrays especially string  
types.

Does anyone know a How-To or anything else?

Thanks

Phil

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Hdf-forum is for HDF software users discussion.  
> Hdf-forum@hdfgroup.org  
> [http://mail.hdfgroup.org/mailman/listinfo/hdf-forum\_hdfgroup.org](http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org)

---

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/philipp_kraus/32/1654_2.png) [@Philipp\_Kraus](https://forum.hdfgroup.org/u/Philipp_Kraus)\
**Post date:** [June 7, 2010, 3:55pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/4 "2010-06-07T15:55:44Z")

</div>

I know these examples and they help me to understand the structures, but I  
don't find string examples and (string) array examples. I have a hdf5 file  
(created with Matlab) and I would like to read in my C++ program. My code:

std::vector\<std::string\> hdf::readStringVector( const std::string& p\_path )  
const  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;H5::DataSet l\_dataset = m\_file.openDataSet( p\_path.c\_str() );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSpace l\_dataspace = l\_dataset.getSpace();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;hsize\_t l\_size[1];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.getSimpleExtentDims( l\_size );   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.close();

&nbsp;&nbsp;std::vector\<std::string\> l\_vec( l\_size[0] );

&nbsp;&nbsp;\*\*\* How can I read the array data   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return l\_vec;  
&nbsp;&nbsp;&nbsp;&nbsp;}

> **···**
>
> --  
> View this message in context: [http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html](http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html)  
> Sent from the hdf-forum mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![mike.jackson](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/mike.jackson/32/1734_2.png) [@mike.jackson](https://forum.hdfgroup.org/u/mike.jackson)\
**Post date:** [June 7, 2010, 4:10pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/5 "2010-06-07T16:10:31Z")

</div>

This is what I use:

//

> **···**
>
> -----------------------------------------------------------------------------  
> // Reads a String dataset  
> // -----------------------------------------------------------------------------  
> herr\_t H5Lite::readStringDataset(hid\_t loc\_id, const std::string& dsetName, std::string &data) {  
> &nbsp;&nbsp;&nbsp;hid\_t did; // dataset id  
> &nbsp;&nbsp;&nbsp;hid\_t tid; //type id  
> &nbsp;&nbsp;&nbsp;herr\_t err = 0;  
> &nbsp;&nbsp;&nbsp;herr\_t retErr = 0;  
> &nbsp;&nbsp;&nbsp;hsize\_t size;
> 
> &nbsp;&nbsp;&nbsp;did = H5Dopen(loc\_id, dsetName.c\_str() );  
> &nbsp;&nbsp;&nbsp;if (did \< 0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Opening Dataset" \<\< std::endl;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;  
> &nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;tid = H5Dget\_type(did);  
> &nbsp;&nbsp;&nbsp;if ( tid \>= 0 ) {
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size = H5Dget\_storage\_size(did);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<char\> buf(static\_cast\<int\>(size+1), 0x00); //Allocate and Zero the array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = H5Dread(did, tid, H5S\_ALL, H5S\_ALL, H5P\_DEFAULT, &(buf.front()) );  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err\<0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Reading string dataset." \<\< std::endl;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.append( &(buf.front()) ); //Append the string to the given string  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;CloseH5D(did, err, retErr);  
> &nbsp;&nbsp;&nbsp;CloseH5T(tid, err, retErr);  
> &nbsp;&nbsp;&nbsp;return retErr;  
> }
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mike Jackson [www.bluequartz.net](http://www.bluequartz.net)  
> Principal Software Engineer mike.jackson@bluequartz.net  
> BlueQuartz Software Dayton, Ohio
> 
> On Jun 7, 2010, at 11:55 AM, Philipp Kraus wrote:
> 
> > I know these examples and they help me to understand the structures, but I  
> > don't find string examples and (string) array examples. I have a hdf5 file  
> > (created with Matlab) and I would like to read in my C++ program. My code:
> > 
> > std::vector\<std::string\> hdf::readStringVector( const std::string& p\_path )  
> > const  
> > &nbsp;&nbsp;&nbsp;{  
> > &nbsp;&nbsp;H5::DataSet l\_dataset = m\_file.openDataSet( p\_path.c\_str() );  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSpace l\_dataspace = l\_dataset.getSpace();
> > 
> > &nbsp;&nbsp;hsize\_t l\_size[1];  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.getSimpleExtentDims( l\_size );   
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.close();
> > 
> > &nbsp;&nbsp;std::vector\<std::string\> l\_vec( l\_size[0] );
> > 
> > \*\*\* How can I read the array data
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return l\_vec;  
> > &nbsp;&nbsp;&nbsp;}
> > 
> > --  
> > View this message in context: [http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html](http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html)  
> > Sent from the hdf-forum mailing list archive at Nabble.com.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Hdf-forum is for HDF software users discussion.  
> > Hdf-forum@hdfgroup.org  
> > [http://mail.hdfgroup.org/mailman/listinfo/hdf-forum\_hdfgroup.org](http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org)

---

<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:** [June 7, 2010, 6:08pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/6 "2010-06-07T18:08:59Z")

</div>

Hi Phil,

You can read the string into an std::string or a char\* buffer then assign it  
to a vector element, or read directly to the vector element, I think. Here  
is an example using the char\* buffer to read a string:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char \*string\_ds;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset.read(&string\_ds, vlst); // string is variable length.  
or  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char string\_ds[size\_of\_your\_string]; // or dynamically allocate it  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset.read(string\_ds, vlst); // string is fixed length.

I hope that helps.  
Binh-Minh

> **···**
>
> -----Original Message-----  
> From: [hdf-forum-bounces@hdfgroup.org](mailto:hdf-forum-bounces@hdfgroup.org) [[mailto:hdf-forum-bounces@hdfgroup.org](mailto:hdf-forum-bounces@hdfgroup.org)]  
> On Behalf Of Philipp Kraus  
> Sent: Monday, June 07, 2010 11:56 AM  
> To: [hdf-forum@hdfgroup.org](mailto:hdf-forum@hdfgroup.org)  
> Subject: Re: [Hdf-forum] HDF5 with C++
> 
> I know these examples and they help me to understand the structures, but I  
> don't find string examples and (string) array examples. I have a hdf5 file  
> (created with Matlab) and I would like to read in my C++ program. My code:
> 
> std::vector\<std::string\> hdf::readStringVector( const std::string& p\_path )  
> const  
> &nbsp;&nbsp;&nbsp;&nbsp;{  
> &nbsp;&nbsp;H5::DataSet l\_dataset = m\_file.openDataSet( p\_path.c\_str() );  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSpace l\_dataspace = l\_dataset.getSpace();  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;hsize\_t l\_size[1];  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.getSimpleExtentDims( l\_size );   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l\_dataspace.close();
> 
> &nbsp;&nbsp;std::vector\<std::string\> l\_vec( l\_size[0] );
> 
> &nbsp;&nbsp;\*\*\* How can I read the array data   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return l\_vec;  
> &nbsp;&nbsp;&nbsp;&nbsp;}
> 
> --  
> View this message in context:  
> [http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html](http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p876706.html)  
> Sent from the hdf-forum mailing list archive at [Nabble.com](http://Nabble.com).
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Hdf-forum is for HDF software users discussion.  
> [Hdf-forum@hdfgroup.org](mailto:Hdf-forum@hdfgroup.org)  
> [http://mail.hdfgroup.org/mailman/listinfo/hdf-forum\_hdfgroup.org](http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org)
> 
> --  
> Internal Virus Database is out-of-date.  
> Checked by AVG.  
> Version: 7.5.549 / Virus Database: 270.9.0/1778 - Release Date: 11/9/2008  
> 2:14 PM

---

<div class="post-metadata">

**Author:** ![Philipp\_Kraus](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/philipp_kraus/32/1654_2.png) [@Philipp\_Kraus](https://forum.hdfgroup.org/u/Philipp_Kraus)\
**Post date:** [June 8, 2010, 7:15pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/7 "2010-06-08T19:15:01Z")

</div>

You mix C and C++ code, but I prefere only C++.

Michael Jackson wrote:

> This is what I use:
> 
> herr\_t H5Lite::readStringDataset(hid\_t loc\_id, const std::string&  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<char\> buf(static\_cast\<int\>(size+1), 0x00); //Allocate  
> and Zero the array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = H5Dread(did, tid, H5S\_ALL, H5S\_ALL, H5P\_DEFAULT,  
> &(buf.front()) );  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err\<0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Reading string dataset." \<\< std::endl;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.append( &(buf.front()) ); //Append the string to the given  
> string

I don't find any native C++ code to read data into the std::vector.

> **···**
>
> --  
> View this message in context: [http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html](http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html)  
> Sent from the hdf-forum mailing list archive at Nabble.com.

---

<div class="post-metadata">

**Author:** ![mike.jackson](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/mike.jackson/32/1734_2.png) [@mike.jackson](https://forum.hdfgroup.org/u/mike.jackson)\
**Post date:** [June 8, 2010, 7:22pm UTC](https://forum.hdfgroup.org/t/hdf5-with-c/1748/8 "2010-06-08T19:22:53Z")

</div>

/\*\*  
&nbsp;&nbsp;\* @brief Reads data from the HDF5 File into an std::vector\<T\> object. If the dataset  
&nbsp;&nbsp;\* is very large this can be an expensive method to use. It is here for convenience  
&nbsp;&nbsp;\* using STL with hdf5.  
&nbsp;&nbsp;\* @param loc\_id The parent location that contains the dataset to read  
&nbsp;&nbsp;\* @param dsetName The name of the dataset to read  
&nbsp;&nbsp;\* @param data A std::vector\<T\>. Note the vector WILL be resized to fit the data.  
&nbsp;&nbsp;\* The best idea is to just allocate the vector but not to size it. The method  
&nbsp;&nbsp;\* will size it for you.  
&nbsp;&nbsp;\* @return Standard HDF error condition  
&nbsp;&nbsp;\*/  
template \<typename T\>  
static herr\_t readVectorDataset(hid\_t loc\_id,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const std::string& dsetName,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<T\> &data)  
{  
&nbsp;&nbsp;&nbsp;hid\_t did;  
&nbsp;&nbsp;&nbsp;herr\_t err = 0;  
&nbsp;&nbsp;&nbsp;herr\_t retErr = 0;  
&nbsp;&nbsp;&nbsp;hid\_t spaceId;  
&nbsp;&nbsp;&nbsp;hid\_t dataType;  
&nbsp;&nbsp;&nbsp;T test = static\_cast\<T\>(0x00);  
&nbsp;&nbsp;&nbsp;dataType = H5Lite::HDFTypeForPrimitive(test);  
&nbsp;&nbsp;&nbsp;if (dataType == -1)  
&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;  
&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;//std::cout \<\< "HDF5 Data Type: " \<\< H5Lite::HDFTypeForPrimitiveAsStr(test) \<\< std::endl;  
&nbsp;&nbsp;/\* Open the dataset. \*/  
// std::cout \<\< " Opening " \<\< dsetName \<\< " for data Retrieval. " \<\< std::endl;  
&nbsp;&nbsp;&nbsp;did = H5Dopen( loc\_id, dsetName.c\_str() );  
&nbsp;&nbsp;&nbsp;if ( did \< 0 ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< " Error opening Dataset: " \<\< did \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;  
&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;if ( did \>= 0 ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spaceId = H5Dget\_space(did);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ( spaceId \> 0 ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int32\_t rank = H5Sget\_simple\_extent\_ndims(spaceId);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (rank \> 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<hsize\_t\> dims;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dims.resize(rank);// Allocate enough room for the dims  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank = H5Sget\_simple\_extent\_dims(spaceId, &(dims.front()), NULL);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hsize\_t numElements = 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (std::vector\<hsize\_t\>::iterator iter = dims.begin(); iter \< dims.end(); ++iter ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numElements = numElements \* (\*iter);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// std::cout \<\< "NumElements: " \<\< numElements \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Resize the vector  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.resize( static\_cast\<int\>(numElements) );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// for (uint32\_t i = 0; i\<numElements; ++i) { data[i] = 55555555; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = H5Dread(did, dataType, H5S\_ALL, H5S\_ALL, H5P\_DEFAULT, &( data.front() ) );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err \< 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Reading Data." \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = H5Sclose(spaceId);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err \< 0 ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Closing Data Space" \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Opening SpaceID" \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = spaceId;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err = H5Dclose( did );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (err \< 0 ) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Closing Dataset" \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;return retErr;  
}

What is the problem mixing C and C++. If you are using HDF5 then you are by definition mixing C and C++.

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mike Jackson [www.bluequartz.net](http://www.bluequartz.net)  
> Principal Software Engineer mike.jackson@bluequartz.net  
> BlueQuartz Software Dayton, Ohio
> 
> On Jun 8, 2010, at 3:15 PM, Philipp Kraus wrote:
> 
> > You mix C and C++ code, but I prefere only C++.
> > 
> > Michael Jackson wrote:
> > 
> > > This is what I use:
> > > 
> > > herr\_t H5Lite::readStringDataset(hid\_t loc\_id, const std::string&  
> > > &nbsp;&nbsp;&nbsp;&nbsp;std::vector\<char\> buf(static\_cast\<int\>(size+1), 0x00); //Allocate  
> > > and Zero the array  
> > > &nbsp;&nbsp;&nbsp;&nbsp;err = H5Dread(did, tid, H5S\_ALL, H5S\_ALL, H5P\_DEFAULT,  
> > > &(buf.front()) );  
> > > &nbsp;&nbsp;&nbsp;&nbsp;if (err\<0) {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Reading string dataset." \<\< std::endl;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retErr = err;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.append( &(buf.front()) ); //Append the string to the given  
> > > string
> > 
> > I don't find any native C++ code to read data into the std::vector.  
> > --  
> > View this message in context: [http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html](http://hdf-forum.184993.n3.nabble.com/HDF5-with-C-tp874249p880263.html)  
> > Sent from the hdf-forum mailing list archive at Nabble.com.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Hdf-forum is for HDF software users discussion.  
> > Hdf-forum@hdfgroup.org  
> > [http://mail.hdfgroup.org/mailman/listinfo/hdf-forum\_hdfgroup.org](http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org)
