Reading a UTF-8 string attribute in C


#1

This seems like a simple question but I have been unable to find documentation on it. I have an attribute whose type is “variable-length null-terminated UTF-8 string”. I would like to read it, using the C API. Does anyone have sample code that does this?


#3

Hi @lacker,

You may want to check HDFql and see if it fits the bill. To read a variable-length UTF8 string in C using HDFql could be done as follows:

// declare variables
char *write;
char *read;

// create an HDF5 file named 'test.h5' and use (i.e. open) it
hdfql_execute("create and use file test.h5");

// allocate memory to variable 'write' and set it to 'dummy'
write = malloc(6);
strcpy(write, "dummy");

// register variable 'write' for subsequent use (by HDFql)
hdfql_variable_transient_register(&write);

// create an HDF5 attribute named 'attrib' of data type variable-length UTF8 string and write content of variable 'write' into it
hdfql_execute("create attribute attrib as utf8 varchar values from memory 0");

// register variable 'read' for subsequent use (by HDFql)
hdfql_variable_transient_register(&read);

// select (i.e. read) attribute 'attrib' and set variable 'read' with its content
hdfql_execute("select from attrib into memory 0");

// print content of variable 'read'
printf("Value=%s\n", read);

#4

I would just like to use the standard library. Surely reading a single string cannot be so difficult that nobody can produce sample code for it.


#5

Does – Read / Write Variable Length String Datatype (Attribute) – h5ex_t_vlstringatt.c

https://support.hdfgroup.org/HDF5/examples/api-c.html

help?


#6

That example appears to be reading a list-of-strings attribute. Is it supposed to work for an attribute that is a single string, as well?


#7

How about this?

h5ex_t_vlstringatt.c (2.2 KB)