read_write_vector_dataset.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 #include <highfive/H5File.hpp>
14 #include <highfive/H5DataSet.hpp>
15 #include <highfive/H5DataSpace.hpp>
16 
17 using namespace HighFive;
18 
19 const std::string FILE_NAME("dataset_integer.h5");
20 const std::string DATASET_NAME("dset");
21 const size_t size_dataset = 20;
22 
23 // create a dataset 1D from a vector of string
24 void write_dataset() {
25 
26  // we create a new hdf5 file
28 
29  std::vector<int> data(size_dataset);
30  for (size_t i = 0; i < data.size(); ++i) {
31  data[i] = int(i);
32  }
33 
34  // let's create a dataset of native integer with the size of the vector
35  // 'data'
36  DataSet dataset =
37  file.createDataSet<int>(DATASET_NAME, DataSpace::From(data));
38 
39  // let's write our vector of int to the HDF5 dataset
40  dataset.write(data);
41 }
42 
43 // read our data back
44 void read_dataset() {
45  // we open the existing hdf5 file we created before
47 
48  std::vector<int> read_data;
49 
50  // we get the dataset
51  DataSet dataset = file.getDataSet(DATASET_NAME);
52 
53  // we convert the hdf5 dataset to a single dimension vector
54  dataset.read(read_data);
55 
56  for (size_t i = 0; i < read_data.size(); ++i) {
57  std::cout << read_data[i] << " ";
58  }
59  std::cout << "\n";
60 }
61 
62 int main(void) {
63 
64  try {
65  write_dataset();
66  read_dataset();
67 
68  } catch (Exception& err) {
69  // catch and print any HDF5 error
70  std::cerr << err.what() << std::endl;
71  }
72 
73  return 0; // successfully terminated
74 }
void read(T &array) const
const size_t size_dataset
const std::string FILE_NAME("dataset_integer.h5")
static DataSpace From(const ScalarValue &scalar_value)
const std::string DATASET_NAME("dset")
void write(const T &buffer)
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
void write_dataset()
static const int ReadOnly
Open flag: Read only access.
Definition: H5File.hpp:30
File class.
Definition: H5File.hpp:25
DataSet getDataSet(const std::string &dataset_name, const DataSetAccessProps &accessProps=DataSetAccessProps()) const
get an existing dataset in the current file
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
FILE * file
int main(void)
void read_dataset()
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space ...
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:09