问题
I'm trying to read the name of a HDF5 DataSet using the C++ API. For H5::Attribute
objects, there is a getName()
method. However, I don't see a similar getName()
method for H5:DataSet
objects.
Ideally I want to do this:
void Dump(H5::DataSet& ds)
{
cout << "Dataset " << ds.getName() << endl;
// continue to print dataset values
}
I know h5dump
can do it, but briefly looking at the code, it only knows it by walking the tree using H5Giterate
, that is only the parent knows the name of the children, but the children don't know their own name.
回答1:
In C, there is H5Iget_name. I couldn't find the equivalent in C++ but you can use DataSet::getId() and give that to the C function.
I guess the reason why this is not as simple as having a getName()
accessor in DataSet
is that to read a dataset, you either need to know its name or walk the tree. The only exception I can think of is when dereferencing a reference to a dataset.
回答2:
This is a partial answer, based on Simon's post. Note that the name is a full hierarchical name,
std::string getName(const H5::DataSet& ds)
{
size_t len = H5Iget_name(ds.getId(),NULL,0);
char buffer[len];
H5Iget_name(ds.getId(),buffer,len+1);
std::string n = buffer;
return n;
}
example name
"/toplevel/videodata"
来源:https://stackoverflow.com/questions/22798731/how-can-one-get-the-name-of-an-hdf5-dataset-through-the-c-or-c-api