# Is there a better way to read a vector of strings?

**URL:** <https://forum.hdfgroup.org/t/is-there-a-better-way-to-read-a-vector-of-strings/2205>\
**Category:** Hdf-forum archives\
**Created:** [June 17, 2011, 3:27am UTC](https://forum.hdfgroup.org/t/is-there-a-better-way-to-read-a-vector-of-strings/2205 "2011-06-17T03:27:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amos\_Anderson](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@Amos\_Anderson](https://forum.hdfgroup.org/u/Amos_Anderson)\
**Post date:** [June 17, 2011, 3:27am UTC](https://forum.hdfgroup.org/t/is-there-a-better-way-to-read-a-vector-of-strings/2205/1 "2011-06-17T03:27:01Z")

</div>

Hello --

Part of my motivation for posting this is just in case someone finds  
it a useful starting point for whatever they're trying to do. I'm a  
newb at hdf5, but I finally figured out how to write a vector of  
strings:

&nbsp;&nbsp;&nbsp;std::vector\<std::string\> confs;  
&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;H5::H5File file (filename, H5F\_ACC\_TRUNC);  
&nbsp;&nbsp;&nbsp;H5::StrType st(H5::PredType::C\_S1, H5T\_VARIABLE);  
&nbsp;&nbsp;&nbsp;hsize\_t dim[1] = {0};  
&nbsp;&nbsp;&nbsp;dim[0] = confs.size();  
&nbsp;&nbsp;&nbsp;H5::DataSpace dataspace = H5::DataSpace(1, dim);  
&nbsp;&nbsp;&nbsp;file.createDataSet(confName.str(),st,dataspace).write(&confs[0],st);

which seems simple enough, so i'm happy with this, unless someone has  
a suggestion... but reading this seems more difficult than necessary  
because I can't figure out if there's an easy way to read the whole  
vector at once, instead of picking them off one at a time:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::H5File file (filename, H5F\_ACC\_RDONLY);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSet dataset = file.openDataSet(confName.str());  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hsize\_t dims[1];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset.getSpace().getSimpleExtentDims(dims, NULL);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<std::string\> conf(dims[0]);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(std::size\_t j=0; j\<dims[0]; j++){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSpace file\_space = dataset.getSpace();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hsize\_t count[1] = {1};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hsize\_t start[1] = {j};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_space.selectHyperslab(H5S\_SELECT\_SET,count,start);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hsize\_t dim1[1] = {1};  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5::DataSpace mem\_space(1,dim1);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset.read(conf[j],st,mem\_space,file\_space);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

note: i've not tried to compile these abstracted code snippets

this seems to work for me... but if there's something better, I prefer  
simple code... any suggestions?

thanks!

amos.
