create_attribute_string_integer.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/H5Attribute.hpp>
14 #include <highfive/H5File.hpp>
15 #include <highfive/H5DataSet.hpp>
16 #include <highfive/H5DataSpace.hpp>
17 
18 using namespace HighFive;
19 
20 const std::string FILE_NAME("create_attribute.h5");
21 const std::string DATASET_NAME("my_dataset");
22 
23 const std::string ATTRIBUTE_NAME_NOTE("note");
24 const std::string ATTRIBUTE_NAME_VERSION("version_string");
25 
26 // create a dataset from a vector of string
27 // read it back and print it
28 int main(void) {
29 
30  try {
31  // Create a new file using the default property lists.
33 
34  // Create a dummy dataset of one single integer
35  DataSet dataset =
37 
38  // Now let's add a attribute on this dataset
39  // This attribute will be named "note"
40  // and have the following content
41  std::string string_list("very important Dataset !");
42 
43  Attribute a = dataset.createAttribute<std::string>(
44  ATTRIBUTE_NAME_NOTE, DataSpace::From(string_list));
45  a.write(string_list);
46 
47  // We also add a "version" attribute
48  // that will be an array 1x2 of integer
49  std::vector<int> version;
50  version.push_back(1);
51  version.push_back(0); // version 1.0
52 
54  DataSpace::From(version));
55  v.write(version);
56 
57  // Ok all attributes are now written
58 
59  // let's list the keys of all attributes now
60  std::vector<std::string> all_attributes_keys =
61  dataset.listAttributeNames();
62  for (std::vector<std::string>::const_iterator it =
63  all_attributes_keys.begin();
64  it < all_attributes_keys.end(); ++it) {
65  std::cout << "attribute: " << *it << std::endl;
66  }
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 }
const std::string DATASET_NAME("my_dataset")
static DataSpace From(const ScalarValue &scalar_value)
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
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
Attribute createAttribute(const std::string &attribute_name, const DataSpace &space, const DataType &type)
create a new attribute with the name attribute_name
FILE * file
void write(const T &buffer)
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
const std::string ATTRIBUTE_NAME_VERSION("version_string")
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 ...
const std::string ATTRIBUTE_NAME_NOTE("note")
const std::string FILE_NAME("create_attribute.h5")
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:06