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 }
const std::string FILE_NAME("create_dataset_string_example.h5")
void read(T &array) const
static DataSpace From(const ScalarValue &scalar_value)
int main(void)
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
File class.
Definition: H5File.hpp:25
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
FILE * file
const std::string DATASET_NAME("story")
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