Created
April 15, 2016 02:41
-
-
Save drhirsch/9f88bbe4841ea67aa692f7395bb38bfe to your computer and use it in GitHub Desktop.
Example of writing and reading file with HDF5 in C. Based on https://scivision.co/linking-libhdf5-dev-with-cmake/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hdf5.h" | |
#define FILE "dset.h5" | |
int main() { | |
hid_t file_id, dataset_id,dataspace_id; /* identifiers */ | |
herr_t status; | |
int i, j, dset_data[4][6], read_data[4][6]; | |
hsize_t dims[2]; | |
/* Initialize the dataset. */ | |
for (i = 0; i < 4; i++) | |
for (j = 0; j < 6; j++) | |
dset_data[i][j] = i * 6 + j + 1; | |
/* Create a new file using default properties. */ | |
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); | |
/* Create the data space for the dataset. */ | |
dims[0] = 4; | |
dims[1] = 6; | |
dataspace_id = H5Screate_simple(2, dims, NULL); | |
/* Create the dataset. */ | |
dataset_id = H5Dcreate2(file_id, "/dset", H5T_STD_I32BE, dataspace_id, | |
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | |
/* Write the dataset. */ | |
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
dset_data); | |
/* End access to the dataset and release resources used by it. */ | |
status = H5Dclose(dataset_id); | |
//------------------------------------------------------ | |
/* Open an existing dataset. */ | |
dataset_id = H5Dopen2(file_id, "/dset", H5P_DEFAULT); | |
status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
read_data); | |
for (i = 0; i < 4; i++) | |
for (j = 0; j < 6; j++) | |
printf("%d ",read_data[i][j]); // 1-24 | |
/* Close the dataset. */ | |
status = H5Dclose(dataset_id); | |
/* Close the file. */ | |
status = H5Fclose(file_id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment