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 }
HighFive::File::ReadOnly
static const int ReadOnly
Open flag: Read only access.
Definition: H5File.hpp:30
FILE_NAME
const std::string FILE_NAME("dataset_integer.h5")
DATASET_NAME
const std::string DATASET_NAME("dset")
size_dataset
const size_t size_dataset
Definition: read_write_vector_dataset.cpp:21
H5File.hpp
write_dataset
void write_dataset()
Definition: read_write_vector_dataset.cpp:24
HighFive::SliceTraits::read
void read(T &array) const
Definition: H5Slice_traits_misc.hpp:149
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
Definition: H5Dataspace_misc.hpp:130
H5DataSet.hpp
H5DataSpace.hpp
HighFive::SliceTraits::write
void write(const T &buffer)
Definition: H5Slice_traits_misc.hpp:210
HighFive::File::Create
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40
HighFive::File
File class.
Definition: H5File.hpp:25
main
int main(void)
Definition: read_write_vector_dataset.cpp:62
read_dataset
void read_dataset()
Definition: read_write_vector_dataset.cpp:44
file
FILE * file
Definition: arithmeticencoder.cpp:77
HighFive::Exception::what
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
HighFive::Exception
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
HighFive::File::ReadWrite
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
HighFive::DataSet
Definition: H5DataSet.hpp:27
HighFive::File::Truncate
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
HighFive
Definition: H5Annotate_traits.hpp:14


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 Wed Mar 2 2022 00:37:24