read_write_dataset_string.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("create_dataset_string_example.h5");
20 const std::string DATASET_NAME("story");
21 
22 // create a dataset from a vector of string
23 // read it back and print it
24 int main(void) {
25 
26  try {
27  // Create a new file using the default property lists.
29 
30  std::vector<std::string> string_list;
31  string_list.push_back("Hello World !");
32  string_list.push_back("This string list is mapped to a dataset of "
33  "variable length string");
34  string_list.push_back(
35  "Encoding is done in UTF-8 - 你好 - Здравствуйте!");
36  string_list.push_back("May the force be with you");
37  string_list.push_back("Enjoy !");
38 
39  // create a dataset ready to contains strings of the size of the vector
40  // string_list
41  DataSet dataset = file.createDataSet<std::string>(
42  DATASET_NAME, DataSpace::From(string_list));
43 
44  // let's write our vector of string
45  dataset.write(string_list);
46 
47  // now we read it back
48  std::vector<std::string> result_string_list;
49  dataset.read(result_string_list);
50 
51  for (size_t i = 0; i < result_string_list.size(); ++i) {
52  std::cout << ":" << i << " " << result_string_list[i] << "\n";
53  }
54 
55  } catch (Exception& err) {
56  // catch and print any HDF5 error
57  std::cerr << err.what() << std::endl;
58  }
59 
60  return 0; // successfully terminated
61 }
main
int main(void)
Definition: read_write_dataset_string.cpp:24
H5File.hpp
FILE_NAME
const std::string FILE_NAME("create_dataset_string_example.h5")
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
DATASET_NAME
const std::string DATASET_NAME("story")
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
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