c++ - 读取 HDFql 中的属性值

标签 c++ hdf5 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,返回值为“t”,如果我说

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

输出变为channels = test/data/channels

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

非常感谢!

最佳答案

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

关于c++ - 读取 HDFql 中的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798074/

相关文章:

python - TensorFlow - tf.data.Dataset 读取大型 HDF5 文件

python - 如何从 pandas hdf5store 中选择 DISTINCT?

c++ - 在 CMake 中链接外部 HDFql 库

python - 有没有办法在Python中逐行写入hdf5文件?

C++ 变量作用域

c++ - C++ 中的 __builtin__functions 有什么用?

c++ - 使用者线程中的函数无法访问内存位置

c++ - 如何通过 COM 变体编码 utf-8 字节串?

c++ - HDFql 保存和加载图像

hdf5 - 处理 HDF5 文件中大量大型二维数组的建议(最佳实践)