2017-01-05 7 views
1

Ich benutze HDF5 CPP-Bibliothek auf Windows-Plattform. Im Grunde möchte ich einen zusammengesetzten Datentyp in eine H5-Datei schreiben, die std :: string enthält.Wie schreibe ich stl :: string zu HDF5-Datei in C++

Dieser Code wird keine Fehler geben, aber während auf H5-Datei wieder schreibt Müll Wert ...

Jungs jeder Hinweis hilfreich ...

Hier ist mein Code: -

#include <H5Cpp.h > 
#include <vector> 
#include <string> 
#include <iostream> 

using namespace std; 
using namespace H5; 


/**Compound datatype with STL Datatype*/ 
struct PostProcessingData 
{ 
    std::string datatype; 
}; 
void main() 
{ 
    H5File file("ResultData.h5",H5F_ACC_TRUNC); 
    Group grp(file.createGroup("PostProcessing")); 

    PostProcessingData data; 
    data.datatype = "stress:xx"; 

    // create spatial structure of data 
    hsize_t len = 1; 
    hsize_t dim[1] = {len}; 
    int rank = 1; 
    DataSpace space(rank,dim); 

    // write required size char array 
    hid_t strtype = H5Tcopy (H5T_C_S1); 
    H5Tset_size (strtype, H5T_VARIABLE); 

    //defining the datatype to pass HDF55 
    H5::CompType mtype(sizeof(PostProcessingData)); 
    mtype.insertMember("Filename", HOFFSET(PostProcessingData, datatype), strtype); 


    DataSet dataset = grp.createDataSet("subc_id2",mtype,space); 
    dataset.write(&data,mtype); 
    space.close(); 
    mtype.close(); 
    dataset.close(); 
    grp.close(); 
    file.close(); 
    exit(0); 
} 
+0

Kann mir bitte hier helfen? –

Antwort

1

Ich habe die folgende Problemumgehung, um eine Zeichenfolge in HDF5 zu speichern: - Ich muss Std :: String in char * Zeiger konvertieren. HDF5 ist sehr gut mit rohen Datentyp.

#include "H5Cpp.h" 
#include <vector> 
#include <string> 
#include <iostream> 


using namespace std; 
using namespace H5; 


/**Compound datatype with STL Datatype*/ 
struct PostProcessingData 
{ 
    char* datatype; 
}; 

void main() 
{ 
    H5File file("ResultData.h5",H5F_ACC_TRUNC); 
    Group grp(file.createGroup("PostProcessing")); 

    PostProcessingData data; 
    std::string dt = "stress:xx"; 
    data.datatype = new char[dt.length()]; 
    data.datatype = const_cast<char*> (dt.data()); 

    // create spatial structure of data 
    hsize_t len = 1; 
    hsize_t dim[1] = {len}; 
    int rank = 1; 
    DataSpace space(rank,dim); 

    // write required size char array 
    hid_t strtype = H5Tcopy (H5T_C_S1); 
    H5Tset_size (strtype, H5T_VARIABLE); 

    //defining the datatype to pass HDF55 
    H5::CompType mtype(sizeof(PostProcessingData)); 
    mtype.insertMember("Filename", HOFFSET(PostProcessingData, datatype), strtype); 


    DataSet dataset = grp.createDataSet("subc_id2",mtype,space); 
    //While writing data to file it gives following error 
    dataset.write(&data,mtype); 
    space.close(); 
    mtype.close(); 
    dataset.close(); 
    grp.close(); 
    file.close(); 
    exit(0); 

} 

Hier Datei Snapshot: - enter image description here