Creating virtual file layer

I’m trying to write data using the core driver and then get its binary representation in memory:

std::vector<int> vec = {42, 10, -7, 8, 2, 15}; //data

// creating virtual file layer:
H5::FileAccPropList fapl;
fapl.setCore(4 * 1024, false);
H5::H5File file("core_file.h5", H5F_ACC_CREAT | H5F_ACC_RDWR, H5::FileCreatPropList::DEFAULT, fapl);
H5::Group group = file.createGroup("/Data");

// init dataspace:
hsize_t dims[1];
dims[0] = vec.size();
H5::DataSpace dataspace(1, dims);

// write data:
H5::DataSet dataset = group.createDataSet("/Data/IntArray", H5::PredType::NATIVE_INT, dataspace);
dataset.write(vec.data(), H5::PredType::NATIVE_INT);

// get file image:         
ssize_t size = H5Fget_file_image(file.getId(), NULL, 0);
std::cout << size << '\n';
std::shared_ptr<uint8_t[]> buf(new uint8_t[size]);
H5Fget_file_image(file.getId(), buf.get(), size);

file.close();

but when trying to convert data from buf as hdf5 format an error occurs:

// writing buf to date.h5
std::fstream stream("data.h5", std::ios::out
 | std::ios::trunc | std::ios::binary);

for (size_t i = 0; i < size; ++i) {
    stream << buf[i];
}
stream.close();

// trying to read hdf5
H5::H5File new_file("data.h5", H5F_ACC_RDONLY);
H5::Group group = new_file.openGroup("/Data");
H5::DataSet dataset = group.openDataSet("/Data/IntArray");

Error:

HDF5-DIAG: Error detected in HDF5 (1.15.0) thread 0:
  #000: /home/anri/Projects/HDF5-test/hdf5/src/H5F.c line 836 in H5Fopen(): unable to synchronously open file
    major: File accessibility
    minor: Unable to open file
  #001: /home/anri/Projects/HDF5-test/hdf5/src/H5F.c line 796 in H5F__open_api_common(): unable to open file
    major: File accessibility
    minor: Unable to open file
  #002: /home/anri/Projects/HDF5-test/hdf5/src/H5VLcallback.c line 3863 in H5VL_file_open(): open failed
    major: Virtual Object Layer
    minor: Can't open object
  #003: /home/anri/Projects/HDF5-test/hdf5/src/H5VLcallback.c line 3675 in H5VL__file_open(): open failed
    major: Virtual Object Layer
    minor: Can't open object
  #004: /home/anri/Projects/HDF5-test/hdf5/src/H5VLnative_file.c line 128 in H5VL__native_file_open(): unable to open file
    major: File accessibility
    minor: Unable to open file
  #005: /home/anri/Projects/HDF5-test/hdf5/src/H5Fint.c line 2039 in H5F_open(): unable to read root group
    major: File accessibility
    minor: Unable to open file
  #006: /home/anri/Projects/HDF5-test/hdf5/src/H5Groot.c line 219 in H5G_mkroot(): can't check if symbol table message exists
    major: Symbol table
    minor: Can't get value
  #007: /home/anri/Projects/HDF5-test/hdf5/src/H5Omessage.c line 789 in H5O_msg_exists(): unable to protect object header
    major: Object header
    minor: Unable to protect metadata
  #008: /home/anri/Projects/HDF5-test/hdf5/src/H5Oint.c line 1003 in H5O_protect(): unable to load object header
    major: Object header
    minor: Unable to protect metadata
  #009: /home/anri/Projects/HDF5-test/hdf5/src/H5AC.c line 1277 in H5AC_protect(): H5C_protect() failed
    major: Object cache
    minor: Unable to protect metadata
  #010: /home/anri/Projects/HDF5-test/hdf5/src/H5Centry.c line 3125 in H5C_protect(): can't load entry
    major: Object cache
    minor: Unable to load metadata into cache
  #011: /home/anri/Projects/HDF5-test/hdf5/src/H5Centry.c line 1189 in H5C__load_entry(): incorrect metadata checksum after all read attempts
    major: Object cache
    minor: Read failed
  #012: /home/anri/Projects/HDF5-test/hdf5/src/H5Ocache.c line 187 in H5O__cache_get_final_load_size(): can't deserialize object header prefix
    major: Object header
    minor: Unable to decode value
  #013: /home/anri/Projects/HDF5-test/hdf5/src/H5Ocache.c line 1100 in H5O__prefix_deserialize(): bad object header version number
    major: Object header
    minor: Wrong version number
terminate called after throwing an instance of 'H5::FileIException'

I’ll check and get back to you…

Could you please try flushing the core file before trying to get the file image?
Also, it’s a good idea to close the dataset/group too if flushing doesn’t help.