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 =
36  file.createDataSet(DATASET_NAME, DataSpace(1), AtomicType<int>());
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 }
main
int main(void)
Definition: create_attribute_string_integer.cpp:28
H5File.hpp
HighFive::AnnotateTraits::createAttribute
Attribute createAttribute(const std::string &attribute_name, const DataSpace &space, const DataType &type)
create a new attribute with the name attribute_name
Definition: H5Annotate_traits_misc.hpp:29
HighFive::DataSpace::From
static DataSpace From(const ScalarValue &scalar_value)
Definition: H5Dataspace_misc.hpp:130
H5DataSet.hpp
ATTRIBUTE_NAME_VERSION
const std::string ATTRIBUTE_NAME_VERSION("version_string")
H5DataSpace.hpp
ATTRIBUTE_NAME_NOTE
const std::string ATTRIBUTE_NAME_NOTE("note")
HighFive::File::Create
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40
FILE_NAME
const std::string FILE_NAME("create_attribute.h5")
HighFive::File
File class.
Definition: H5File.hpp:25
HighFive::AtomicType
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:41
HighFive::Attribute
Definition: H5Attribute.hpp:23
file
FILE * file
Definition: arithmeticencoder.cpp:77
HighFive::AnnotateTraits::listAttributeNames
std::vector< std::string > listAttributeNames() const
list all attribute name of the node / group
Definition: H5Annotate_traits_misc.hpp:88
H5Attribute.hpp
HighFive::DataSpace
Definition: H5DataSpace.hpp:30
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
DATASET_NAME
const std::string DATASET_NAME("my_dataset")
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
HighFive::Attribute::write
void write(const T &buffer)
Definition: H5Attribute_misc.hpp:93


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:23