HDF5 Compatibility with STL Datatype

Hi Guys,

I am able write STL datatype using .data() function. Example vector.data().

But, That datatype is part of some compound datatype then I am not able to
write exact values(It writes some Garbage Values)

I have simplified my problem can anyone please take look on that …

Here Is my Data Structure

struct RandomData

{

               std::vector<float> values;

};

I am using HDF5 CPP library for writing above data structure to HDF file. I
am able write file but It’s writes some “Garbage Values”.

Here Is code …

#include "H5Cpp.h"

#include <vector>

using namespace std;

using namespace H5;

void main()

{

       try

       {

             struct RandomData

             {

                std::vector<float> values;

             };

             RandomData d;

             d.values.push_back(10);

             d.values.push_back(20);

             d.values.push_back(30);

             //write Dataset to File

             H5File file("file.h5",H5F_ACC_TRUNC);

             Group group = file.createGroup("group");

             int rank = 1;

             hsize_t dimesion [] = {1};

             DataSpace space(rank,dimesion);

             CompType comptype(sizeof(RandomData));

              comptype.insertMember("x"
,HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

             DataSet dataset = group.createDataSet("r1",comptype,space);

             dataset.write(&d,comptype);

             space.close();

             dataset.close();

             group.close();

             file.close();

       }

       catch(FileIException error)

       {

             error.printError();

       }

}

Am I missing something over here? If not then please guide me on
alternative solution .It’s very helpful for me.

Regards,

Shamkumar

A std::vector<float> will look something like this in memory:

vector<float> {
   float* buf;
   size_t size;
   size_t capactiy;
};

So that's what is getting written by your code. The data you actually want
to write is where the 'buf' pointer points (which is what vector::data()
returns).

If you want to put an array of data into a struct you can use
std::array<float> but that will have fixed size. There is no way to put a
dynamically sized array into a struct.

···

On Tue, May 3, 2016 at 12:48 AM, shamkumar rajput < shamkumar.rajput@gmail.com> wrote:

Hi Guys,

I am able write STL datatype using .data() function. Example
vector.data().

But, That datatype is part of some compound datatype then I am not able to
write exact values(It writes some Garbage Values)

I have simplified my problem can anyone please take look on that …

Here Is my Data Structure

struct RandomData

{

               std::vector<float> values;

};

I am using HDF5 CPP library for writing above data structure to HDF file.
I am able write file but It’s writes some “Garbage Values”.

Here Is code …

#include "H5Cpp.h"

#include <vector>

using namespace std;

using namespace H5;

void main()

{

       try

       {

             struct RandomData

             {

                std::vector<float> values;

             };

             RandomData d;

             d.values.push_back(10);

             d.values.push_back(20);

             d.values.push_back(30);

             //write Dataset to File

             H5File file("file.h5",H5F_ACC_TRUNC);

             Group group = file.createGroup("group");

             int rank = 1;

             hsize_t dimesion [] = {1};

             DataSpace space(rank,dimesion);

             CompType comptype(sizeof(RandomData));

              comptype.insertMember("x"
,HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

             DataSet dataset = group.createDataSet("r1",comptype,space);

             dataset.write(&d,comptype);

             space.close();

             dataset.close();

             group.close();

             file.close();

       }

       catch(FileIException error)

       {

             error.printError();

       }

}

Am I missing something over here? If not then please guide me on
alternative solution .It’s very helpful for me.

Regards,

Shamkumar

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

Hello Shamkumar,

The problem is that H5::PredType::NATIVE_FLOAT should only be used with a single float rather than with an array of floats.

The datatype in this statement

  comptype.insertMember("x",HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

would have to reflex the multiple values of the RandomData::values.

You would need to create an HDF5 datatype to represent the type of RandomData::values. I would guess an ArrayType, then pass that in place of H5::PredType::NATIVE_FLOAT. The length of the array would be 3.

I hope this helps.

Binh-Minh

···

________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of shamkumar rajput <shamkumar.rajput@gmail.com>
Sent: Tuesday, May 3, 2016 3:48 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] HDF5 Compatibility with STL Datatype

Hi Guys,

I am able write STL datatype using .data() function. Example vector.data().
But, That datatype is part of some compound datatype then I am not able to write exact values(It writes some Garbage Values)

I have simplified my problem can anyone please take look on that ...

Here Is my Data Structure

struct RandomData
{
               std::vector<float> values;
};

I am using HDF5 CPP library for writing above data structure to HDF file. I am able write file but It's writes some "Garbage Values".

Here Is code ...

#include "H5Cpp.h"
#include <vector>
using namespace std;
using namespace H5;

void main()
{
       try
       {
             struct RandomData
             {
                std::vector<float> values;
             };
             RandomData d;
             d.values.push_back(10);
             d.values.push_back(20);
             d.values.push_back(30);

             //write Dataset to File
             H5File file("file.h5",H5F_ACC_TRUNC);
             Group group = file.createGroup("group");
             int rank = 1;
             hsize_t dimesion [] = {1};
             DataSpace space(rank,dimesion);
             CompType comptype(sizeof(RandomData));

              comptype.insertMember("x",HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

             DataSet dataset = group.createDataSet("r1",comptype,space);
             dataset.write(&d,comptype);

             space.close();
             dataset.close();
             group.close();
             file.close();
       }
       catch(FileIException error)
       {
             error.printError();
       }
}

Am I missing something over here? If not then please guide me on alternative solution .It's very helpful for me.

Regards,
Shamkumar

Hi Guys,

Thanks you for quick reply.

Both solution are working fine for me.

Regards,
Shamkumar

···

On Tue, May 3, 2016 at 10:21 PM, David <list@aue.org> wrote:

A std::vector<float> will look something like this in memory:

vector<float> {
   float* buf;
   size_t size;
   size_t capactiy;
};

So that's what is getting written by your code. The data you actually want
to write is where the 'buf' pointer points (which is what vector::data()
returns).

If you want to put an array of data into a struct you can use
std::array<float> but that will have fixed size. There is no way to put a
dynamically sized array into a struct.

On Tue, May 3, 2016 at 12:48 AM, shamkumar rajput < > shamkumar.rajput@gmail.com> wrote:

Hi Guys,

I am able write STL datatype using .data() function. Example
vector.data().

But, That datatype is part of some compound datatype then I am not able
to write exact values(It writes some Garbage Values)

I have simplified my problem can anyone please take look on that …

Here Is my Data Structure

struct RandomData

{

               std::vector<float> values;

};

I am using HDF5 CPP library for writing above data structure to HDF file.
I am able write file but It’s writes some “Garbage Values”.

Here Is code …

#include "H5Cpp.h"

#include <vector>

using namespace std;

using namespace H5;

void main()

{

       try

       {

             struct RandomData

             {

                std::vector<float> values;

             };

             RandomData d;

             d.values.push_back(10);

             d.values.push_back(20);

             d.values.push_back(30);

             //write Dataset to File

             H5File file("file.h5",H5F_ACC_TRUNC);

             Group group = file.createGroup("group");

             int rank = 1;

             hsize_t dimesion [] = {1};

             DataSpace space(rank,dimesion);

             CompType comptype(sizeof(RandomData));

              comptype.insertMember("x"
,HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

             DataSet dataset = group.createDataSet("r1",comptype,space);

             dataset.write(&d,comptype);

             space.close();

             dataset.close();

             group.close();

             file.close();

       }

       catch(FileIException error)

       {

             error.printError();

       }

}

Am I missing something over here? If not then please guide me on
alternative solution .It’s very helpful for me.

Regards,

Shamkumar

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

Shamkumar, I meant "reflect", not "reflex". Sorry!

Binh-Minh

···

________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of Binh-Minh Ribler <bmribler@hdfgroup.org>
Sent: Tuesday, May 3, 2016 10:58 AM
To: HDF Users Discussion List
Subject: Re: [Hdf-forum] HDF5 Compatibility with STL Datatype

Hello Shamkumar,

The problem is that H5::PredType::NATIVE_FLOAT should only be used with a single float rather than with an array of floats.

The datatype in this statement

  comptype.insertMember("x",HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

would have to reflex the multiple values of the RandomData::values.

You would need to create an HDF5 datatype to represent the type of RandomData::values. I would guess an ArrayType, then pass that in place of H5::PredType::NATIVE_FLOAT. The length of the array would be 3.

I hope this helps.

Binh-Minh

________________________________
From: Hdf-forum <hdf-forum-bounces@lists.hdfgroup.org> on behalf of shamkumar rajput <shamkumar.rajput@gmail.com>
Sent: Tuesday, May 3, 2016 3:48 AM
To: hdf-forum@lists.hdfgroup.org
Subject: [Hdf-forum] HDF5 Compatibility with STL Datatype

Hi Guys,

I am able write STL datatype using .data() function. Example vector.data().
But, That datatype is part of some compound datatype then I am not able to write exact values(It writes some Garbage Values)

I have simplified my problem can anyone please take look on that ...

Here Is my Data Structure

struct RandomData
{
               std::vector<float> values;
};

I am using HDF5 CPP library for writing above data structure to HDF file. I am able write file but It's writes some "Garbage Values".

Here Is code ...

#include "H5Cpp.h"
#include <vector>
using namespace std;
using namespace H5;

void main()
{
       try
       {
             struct RandomData
             {
                std::vector<float> values;
             };
             RandomData d;
             d.values.push_back(10);
             d.values.push_back(20);
             d.values.push_back(30);

             //write Dataset to File
             H5File file("file.h5",H5F_ACC_TRUNC);
             Group group = file.createGroup("group");
             int rank = 1;
             hsize_t dimesion [] = {1};
             DataSpace space(rank,dimesion);
             CompType comptype(sizeof(RandomData));

              comptype.insertMember("x",HOFFSET(RandomData,values),H5::PredType::NATIVE_FLOAT);

             DataSet dataset = group.createDataSet("r1",comptype,space);
             dataset.write(&d,comptype);

             space.close();
             dataset.close();
             group.close();
             file.close();
       }
       catch(FileIException error)
       {
             error.printError();
       }
}

Am I missing something over here? If not then please guide me on alternative solution .It's very helpful for me.

Regards,
Shamkumar