I'm trying to write data to a dataset consisting of a 1-by-4 array of
integers, but I want to write the integers one-by-one, appending an
integer to the existing dataset each time through the loop. I'm using a
hyperslab to select where I am writing, and then writing the integer
(four writes total). This simple example simulates the way I will
(eventually) be writing more complex real-time data that is streaming
in.
Everything I've read so far indicates that the following code should
work. Am I missing an obvious step? Why is only the first element being
written correctly?
Thanks in advance - I appreciate any feedback and am looking to learn
// Create file descriptor for HDF output file
hid_t file = H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT,
H5P_DEFAULT);
hid_t dataset, datatype, dataspace;
// Initial hyperslab selection will be the first element (0,0)
hsize_t start[] = {0,0};
hsize_t count[] = {1,1};
// Make the dataset dimensions 1-by-4
hsize_t dims[] = {1, 4};
// Create the file dataspace
dataspace = H5Screate_simple(2, dims, NULL);
// Create the dataset
dataset = H5Dcreate(file, "/dset", H5T_STD_I32LE, dataspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
// Create buffer
int *ii = new int;
for (int i = 0; i < 4; i++) {
// Set value of buffer (first time is 15, then 16, etc)
*ii = i+15;
// Select 1-by-1 hyperslab to write to, starting at the
first element (0,0)
start[1] = i;
H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start,
NULL, count, NULL);
// Write data
H5Dwrite(dataset, H5T_STD_I32LE, H5S_ALL, dataspace,
H5P_DEFAULT, ii);
}
// Clean up
H5Dclose(dataset);
H5Sclose(dataspace);
H5Fclose(file);
// I expect this to create the following 1-by-4 dataset of
integer elements:
//
// | 15 | 16 | 17 | 18 |
//
// Instead, it only seems to write to the first element:
//
// | 15 | -1219428236 | 0 | 17 |
//
// In fact, even if skip the first element, and initially set
start to {0, 1}, it still won't
// let me write to any of the other elements:
//
// | 0 | -1219428236 | 0 | 17 |
//
// What is going on? FYI: I mostly followed the code written in
the HDF User Guide
···
from my mistakes.