Medusa  1.1
Coordinate Free Mehless Method implementation
CSV.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_IO_CSV_HPP_
2 #define MEDUSA_BITS_IO_CSV_HPP_
3 
9 #include "CSV_fwd.hpp"
10 #include <fstream>
11 #include <cstring>
12 #include <exception>
13 #include <string>
14 #include <iomanip>
15 
16 namespace mm {
17 
18 template <typename arr_t>
19 void CSV::write(const std::string& filename, const arr_t& array) {
20  std::ofstream f(filename);
21  assert_msg(f.good(), "Error opening CSV file '%s': %s.", filename, strerror(errno));
22  f << std::setprecision(16);
23  for (const auto& x : array) {
24  f << x << '\n';
25  }
26 }
27 
28 template <typename arr_t>
29 void CSV::write2d(const std::string& filename, const arr_t& array, char separator) {
30  std::ofstream f(filename);
31  assert_msg(f.good(), "Error opening CSV file '%s': %s.", filename, strerror(errno));
32  f << std::setprecision(16);
33  for (const auto& x : array) {
34  bool first = true;
35  for (const auto& y : x) {
36  if (!first) f << separator;
37  else first = false;
38  f << y;
39  }
40  f << '\n';
41  }
42 }
43 
44 } // namespace mm
45 
46 #endif // MEDUSA_BITS_IO_CSV_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
CSV_fwd.hpp
mm::CSV::write
static void write(const std::string &filename, const arr_t &array)
Writes given array to a CSV file (as a column).
Definition: CSV.hpp:19
mm::CSV::write2d
static void write2d(const std::string &filename, const arr_t &array, char separator=',')
Writes given 2d array to a CSV file.
Definition: CSV.hpp:29