Hello,

I was expecting that by not inserting a field into memtype, I could avoid
outputing that field to disk. However, the attached test file shows that
this is not the case. Even if I do not insert the field, everything is
written and everything is read back as well.

I guess it has to do with the size of memtype. I tried pack() to reduce the
size but the data interpretation then went wrong.

Defining a new struct containing only these wanted fields is not optimal,
since it would require copying the data to the new struct or back, while my
application involves huge amounts of data. What I try to hide is actually a
vector field, which I write out separately as an array of variable-length
arrays. Currently although I have omitted the vector field in memtype, it
is still written and then read back as well, which corrupts the memory (the
reading automatically fills the size and memory pointer of the vectors with
their written values, which are not valid pointers anymore).

So is there a way that I can truly hide a particular field from being
written as well as from being read back, without having to define a new
temporary class?

Thank you!

Jiaxin
#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#include <vector>

#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
using std::vector;
#endif  // H5_NO_STD
#endif
#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

const H5std_string FILE_NAME( "test_compound2.hdf5" );
const H5std_string DATASET_NAME( "data" );
const int   LENGTH = 5;
const int   RANK = 1;

#define ShowField(s,f){\
	cout << endl<<"Field "<<#f<<" : " << endl; \
	for(int i = 0; i < LENGTH; i++)\
	  cout<<s[i].f<<" ";\
	  cout<<endl;\
	}
	
int main(void)
{

    struct s_t
    {
        int    a;
        float  b;
        int c;
    };
    CompType mtype( sizeof(s_t) );
    /*only insert a,b, do not insert c*/
    mtype.insertMember( "a", HOFFSET(s_t, a), PredType::NATIVE_INT);
    mtype.insertMember( "b", HOFFSET(s_t, b), PredType::NATIVE_FLOAT);
	/*note field c is not inserted!*/

    hsize_t dim[] = {LENGTH};
    vector <s_t> datain(LENGTH);
    for(int i=0; i<LENGTH; i++)/* init data*/
    {
        datain[i].a=i;
        datain[i].b=i*i;
        datain[i].c=-i;
    }
    cout<<"==========Data initialized=============\n";
	ShowField(datain, a);
	ShowField(datain, b);
	ShowField(datain, c);
	
    /*write to file*/
    {
        DataSpace space( RANK, dim );
        H5File file( FILE_NAME, H5F_ACC_TRUNC );
        DataSet dset(file.createDataSet(DATASET_NAME, mtype, space));
        dset.write( datain.data(), mtype );
    }

    /*read back*/
    H5File file( FILE_NAME, H5F_ACC_RDONLY );
    DataSet dset(file.openDataSet( DATASET_NAME ));
    vector <s_t> dataout(LENGTH);

	dset.read( dataout.data(), mtype );

    cout<<"\n===========Data Read==========\n";
    ShowField(dataout,a);
    ShowField(dataout,b);
    ShowField(dataout,c); 
	
	cout<<"\nfield c is read back correctly, though not inserted in memtype!\n";
/*Output:
==========Data initialized=============

Field a : 
0 1 2 3 4 

Field b : 
0 1 4 9 16 

Field c : 
0 -1 -2 -3 -4 

===========Data Read==========

Field a : 
0 1 2 3 4 

Field b : 
0 1 4 9 16 

Field c : 
0 -1 -2 -3 -4 
*/
    return 0;
}
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

Reply via email to