读取 HDFql 中的属性值

Read Value of Attribute in HDFql

我正在使用 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,return 值是 "t",如果我说

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

输出变为channels = test/data/channels

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

非常感谢!

当您执行 SHOW ATTRIBUTE test/data/channels 时,您基本上是在测试存储在 test/data 中的名为 channels 的属性是否存在。由于此属性存在,函数 HDFql::execute returns 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;