I'm looking at this example
http://hdf-forum.184993.n3.nabble.com/C-API-how-to-write-string-attribute-t
t723857.html
Which shows me how to write a single string into an HD5 file.
I'm not seeing exactly how I would write out a list of strings using the
C++ API.
Sketching it out, I was following the example for creating datasets of
scalars, but
I get stuck at how to actually write into the dataset.
void
WriteVariableLengthStringVector(H5::H5File &output,
const std::string &setName,
std::vector<std::string> &stringList)
{
hsize_t numStrings(stringList.size());
H5::DataSpace strSpace(1,&numStrings);
H5::StrType strType(0,H5T_VARIABLE);
H5::DataSet strSet = output.createDataSet("SubjectIDs",strType,strSpace);
//
// what's supposed to happen now?
strSet.write(???,H5::PredType::C_S1);
}
···
________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________
Well I know now that the code below doesn't work. It doesn't like the data
type, I pass in apparently.
HDF5-DIAG: Error detected in HDF5 (1.8.7-snap2) thread 0:
#000: HDF5/src/H5Dio.c line 266 in H5Dwrite(): can't write data
major: Dataset
minor: Write failed
#001: HDF5/src/H5Dio.c line 541 in H5D_write(): unable to set up type
info
major: Dataset
minor: Unable to initialize object
#002: HDF5/src/H5Dio.c line 836 in H5D_typeinfo_init(): unable to
convert between src and dest datatype
major: Dataset
minor: Feature is unsupported
#003: HDF5/src/H5T.c line 4454 in H5T_path_find(): no appropriate
function for conversion path
major: Datatype
minor: Unable to initialize object
terminate called after throwing an instance of 'H5::DataSetIException'
void WriteArray(H5::H5File &output,const std::string &setName,
std::vector<std::string> &subjectNames)
{
hvl_t *ids = new hvl_t[subjectNames.size()];
{ // initialize variable length array
int i = 0;
for(std::vector<std::string>::iterator it = subjectNames.begin();
it != subjectNames.end(); it++)
{
ids[i].len = (*it).size()+1;
ids[i].p = static_cast<void *>(new char[ids[i].len]);
strcpy(static_cast<char *>(ids[i].p),(*it).c_str());
i++;
}
}
hsize_t numStrings(subjectNames.size());
H5::DataSpace strSpace(1,&numStrings);
H5::StrType strType(0,H5T_VARIABLE);
H5::DataSet strSet =
output.createDataSet("/SubjectIDs",strType,strSpace);
strSet.write(ids,H5::PredType::C_S1);
for(int i = 0; i < numStrings; i++)
{
delete [] static_cast<char *>(ids[i].p);
}
delete [] ids;
}
···
________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________
I worked it out for myself. This function below actually works.
static
void
WriteHDFStringList(H5::H5File &file,
const char *name,
std::vector<std::string> &stringList)
{
hsize_t numStrings(stringList.size());
char **stringListCstr = new char *[numStrings];
{
int i = 0;
for(std::vector<std::string>::iterator it = stringList.begin();
it != stringList.end(); it++)
{
stringListCstr[i] = new char[it->size()+1];
strcpy(stringListCstr[i],it->c_str());
i++;
}
}
H5::DataSpace strSpace(1,&numStrings);
H5::StrType strType(H5::PredType::C_S1,H5T_VARIABLE);
H5::DataSet strSet = file.createDataSet(name,strType,strSpace);
strSet.write(stringListCstr,strType);
for(unsigned int i = 0; i < numStrings; i++)
{
delete [] stringListCstr[i];
}
delete [] stringListCstr;
}
···
________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged. If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited. Please reply to the sender that you have received the message in error, then delete it. Thank you.
________________________________