Hello. I want to read attributes with compound datatypes without having to create a struct because my file contains many different compound datatypes and creating structs will be infeasible. I believe this is possible because h5py is able to read compound datatypes for attributes and it is a pythonic wrapper over the C API. How should I go about doing this using C?
Hi @raffayatiq2341,
To be able to read compound attributes without having structs defined a priori in C, one needs to usually perform several steps. Abstractly, here are the steps:
- Get the size of the compound attribute in bytes.
- Get the number of elements that the compound attribute stores.
- Allocate memory (through, e.g.,
malloc
) with a size equal to the value calculated in step1
x the value calculated in step2
. - Read the compound attribute and populate the memory allocated in step
3
with it. - Get the members’ offsets (in memory) of the compound attribute.
- Process allocated memory using members’ offsets from step
5
to know where each member’s data is located.
As one can see, this might be a rather laborious and error-prone task, especially in C. To alleviate, one may use HDFql that greatly simplifies the implementation of the steps described above. In addition, one can use HDFql cursors to solve this use-case in an even simpler way. To illustrate:
int data_type;
hdfql_execute("SELECT from my_attribute");
while (hdfql_cursor_next(NULL) == HDFQL_SUCCESS)
{
data_type = hdfql_cursor_get_data_type(NULL);
if (data_type == HDFQL_INT)
{
printf("The member is an int and its values is %d\n", *hdfql_cursor_get_int(NULL));
}
else if (data_type == HDFQL_FLOAT)
{
printf("The member is a float and its values is %f\n", *hdfql_cursor_get_float(NULL));
}
else if (data_type == HDFQL_DOUBLE)
{
printf("The member is a double and its values is %f\n", *hdfql_cursor_get_double(NULL));
}
}