2016-11-21 7 views
1

값이 가변 char *의 가변 길이 배열 인 스칼라 값 속성을 성공적으로 만들었습니다. 그러나 나는이 속성을 읽는 방법을 이해하지 못한다! 이것은 내가 속성을 만드는 데 사용되는 코드입니다값이 가변 길이 char * (즉 c_strings?)의 배열 인 HDF5 스칼라 속성을 읽는 방법

:이 데이터를 읽는 방법을 이해하지

HDF5 "d:\tmp\hdf5_tutorial\h5tutr_dset.h5" { 
GROUP "/" { 
DATASET "dset" { 
DATATYPE H5T_STD_I32BE 
DATASPACE SIMPLE { (4, 6)/(4, 6) } 
DATA { 
(0,0): 1, 7, 13, 19, 25, 31, 
(1,0): 2, 8, 14, 20, 26, 32, 
(2,0): 3, 9, 15, 21, 27, 33, 
(3,0): 4, 10, 16, 22, 28, 34 
} 
ATTRIBUTE "multi_filters" { 
DATATYPE H5T_VLEN { H5T_STRING { 
STRSIZE H5T_VARIABLE; 
STRPAD H5T_STR_NULLTERM; 
CSET H5T_CSET_ASCII; 
CTYPE H5T_C_S1; 
}} 
DATASPACE SCALAR 
DATA { 
(0): ("this is 0", "this is 1", "this is 2", "this is 3", "this is 4", "this is 5", "this is 6", "this is 7", "this is 8", "this is 9") 
} 
} 
} 
} 
} 

: 여기

void create_attribute_with_vector_of_strings_as_value() 
{ 
    using namespace H5; 

    // Create some test strings. 
    std::vector<std::string> strings; 
    for (int iii = 0; iii < 10; iii++) 
    { 
     strings.push_back("this is " + boost::lexical_cast<std::string>(iii)); 
    } 

    // Part 1: grab pointers to the chars 
    std::vector<const char*> chars; 
    for (auto si = strings.begin(); si != strings.end(); ++si) 
    { 
     std::string &s = (*si); 
     chars.push_back(s.c_str()); 
    } 
    BOOST_TEST_MESSAGE("Size of char* array is: " << chars.size()); 

    // Part 2: create the variable length type 
    hvl_t hdf_buffer; 
    hdf_buffer.p = chars.data(); 
    hdf_buffer.len = chars.size(); 

    // Part 3: create the type 
    auto s_type = H5::StrType(H5::PredType::C_S1, H5T_VARIABLE); 
    auto svec_type = H5::VarLenType(&s_type); 

    try 
    { 
     // Open an existing file and dataset. 
     H5File file(m_file_name.c_str(), H5F_ACC_RDWR); 

     // Part 4: write the output to a scalar attribute 
     DataSet dataset = file.openDataSet(m_dataset_name.c_str()); 

     std::string filter_names = "multi_filters"; 

     Attribute attribute = dataset.createAttribute(filter_names.c_str(), svec_type, H5S_SCALAR); 
     attribute.write(svec_type, &hdf_buffer); 
     file.close(); 
    } 

는 h5dump에서 본 속성을 가진 데이터 세트입니다. 지금까지 실험 해본 코드는 다음과 같습니다. 그것은 컴파일하지만 배열 길이를 알려진 길이로 고정 시켰고 가변 길이 cstring은 비어 있습니까? 누구든지 내가 잘못 가고있는 것에 대한 제안이 있습니까? 특히 const char * 배열의 길이를 쿼리하는 방법과 배열에 포함 된 실제 const char * cstrings을 읽는 방법은 무엇입니까? 당신은 당신이 생각하고있는 것을 구현하기 위해 특정 기술을 사용할 필요가 없습니다 경우

void read_attribute_with_vector_of_strings_as_value() 
{ 
    using namespace H5; 

    std::vector<std::string> strings; 

    try 
    { 
     // Open an existing file and dataset readonly 
     H5File file(m_file_name.c_str(), H5F_ACC_RDONLY); 

     // Part 4: Open the dataset 
     DataSet dataset = file.openDataSet(m_dataset_name.c_str()); 

     // Atribute_name 
     std::string filter_names = "multi_filters"; 

     Attribute attribute = dataset.openAttribute(filter_names.c_str()); 
     size_t sz = attribute.getInMemDataSize(); 
     size_t sz_1 = attribute.getStorageSize(); 
     auto t1 = attribute.getDataType(); 
     VarLenType t2 = attribute.getVarLenType(); 
     H5T_class_t type_class = attribute.getTypeClass(); 
     if (type_class == H5T_STRING) 
      BOOST_TEST_MESSAGE("H5T_STRING"); 

     int length = 10; 
     std::vector<char*> tmp_vec(length); 
     auto s_type = H5::StrType(H5::PredType::C_S1, H5T_VARIABLE); 
     auto svec_type = H5::VarLenType(&s_type); 

     hvl_t hdf_buffer; 
     hdf_buffer.p = tmp_vec.data(); 
     hdf_buffer.len = length; 
     attribute.read(svec_type, &hdf_buffer); 
     //attribute.read(s_type, &hdf_buffer); 
     //attribute.read(tmp_vec.data(), s_type); 

     for(size_t x = 0; x < tmp_vec.size(); ++x) 
     { 
      fprintf(stdout, "GOT STRING [%s]\n", tmp_vec[x]); 
      strings[x] = tmp_vec[x]; 
     } 

     file.close(); 
    } 

답변

0

, 당신은 HDFql HDF 쉽게 (SQL 생각) 파일을 관리 할 수있는 높은 수준의 언어입니다 (http://www.hdfql.com)을 고려할 수 있습니다. 그렇게하면 설명하는 HDF 파일을 조작하는 모든 하위 수준의 세부 사항에서 완화 할 수 있습니다. C++에서 HDFql을 사용하여 가변 길이 char 배열을 읽는 작업은 다음과 같이 수행됩니다.

// include HDFql C++ header file (make sure it can be found by the C++ compiler) 
#include <iostream> 
#include "HDFql.hpp" 

int main(int argc, char *argv[]) 
{ 

    // create an HDF file named "example.h5" and use (i.e. open) it 
    HDFql::execute("CREATE FILE example.h5"); 
    HDFql::execute("USE FILE example.h5"); 

    // create an attribute named "multi_filters" of type varchar of one dimension (size 5) 
    HDFql::execute("CREATE ATTRIBUTE multi_filters AS VARCHAR(5)"); 

    // insert (i.e. write) values "Red", "Green", "Blue", "Orange" and "Yellow" into attribute "multi_filters" 
    HDFql::execute("INSERT INTO multi_filters VALUES(Red, Green, Blue, Orange, Yellow)"); 

    // select (i.e. read) attribute "multi_filters" into HDFql default cursor 
    HDFql::execute("SELECT FROM multi_filters"); 

    // display content of HDFql default cursor 
    while(HDFql::cursorNext() == HDFql::Success) 
    { 
     std::cout << "Color " << HDFql::cursorGetChar() << " has a size of " << HDFql::cursorGetSize() << std::endl; 
    } 

    return 0; 

}