0

我正在使用HDFql创建一个 HDF5 文件。我正在创建一个组并将一些数据放入其中,效果很好。然后我向文件添加一个属性(这似乎也有效,因为当我使用 HDF 编辑器检查 HDF5 文件时它会显示出来),但我不知道如何读取属性的值。这是一个最小的例子:

#include <iostream.h>
#include <HDFql.hpp>

int main (int argc, const char * argv[]) {
    char script[1024];
    //Create the HDF5 file and the group "test"
    HDFql::execute("CREATE TRUNCATE FILE /tmp/test.h5");
    HDFql::execute("USE FILE /tmp/test.h5");
    HDFql::execute("CREATE GROUP test");

    //Generate some arbitrary data and place it in test/data
    int data_length = 1000;
    int data[data_length];
    for(int i=0; i<data_length; i++) {data[i] = i;}
    sprintf(script, "CREATE DATASET test/data AS INT(%d) VALUES FROM MEMORY %d", 
        data_length, HDFql::variableTransientRegister(data));
    HDFql::execute(script);

    //Create an attribute called "channels" and give it an arbitrary value of 11
    HDFql::execute("CREATE ATTRIBUTE test/data/channels AS INT VALUES(11)");

    //Show the attribute
    HDFql::execute("SHOW ATTRIBUTE test/data/channels");
    //Try to move the cursor to the attribute
    HDFql::cursorLast();
    //If that worked, print the attribute contents
    if(HDFql::cursorGetInt())
    {
        std::cout << "channels = " << *HDFql::cursorGetInt() << std::endl;
    } else
    {
        std::cout << "Couldn't find attribute" << std::endl;
    }

    HDFql::execute("CLOSE FILE");
}

我希望控制台的输出是channels = 11,而不是我得到channels = 1953719668。奇怪的是,如果我cursorGetChar改为调用,返回值是“t”,如果我说

std::cout << "channels = " << HDFql::cursorGetChar() << std::endl;

输出变为channels = test/data/channels.

所以我想我误解了 HDFql 游标的工作原理。所以我的问题是:我的代码有什么问题?为什么我的代码错了?

非常感谢!

4

1 回答 1

1

当你这样做时,SHOW ATTRIBUTE test/data/channels你基本上是在测试一个名为channels存储在test/data. 由于此属性存在,因此函数HDFql::execute返回HDFql::Success并且光标填充了字符串test/data/channels。另一方面,如果该属性不存在,则函数HDFql::execute将返回HDFql::ErrorNotFound并且光标将为空。

要读取存储在属性中的值,test/data/channels请改为执行以下操作:

HDFql::execute("SELECT FROM test/data/channels");
HDFql::cursorFirst();
std::cout << "channels = " << *HDFql::cursorGetInt() << std::endl;
于 2019-09-05T13:33:18.790 回答