Motion Master
Loading...
Searching...
No Matches
csv_writer.h
Go to the documentation of this file.
1#pragma once
2
3#include <fstream>
4#include <sstream>
5#include <string>
6
7class CSVWriter {
8 public:
9 explicit CSVWriter(std::string delimiter = ",") : delimiter_(delimiter) {}
10
11 template <typename T>
12 void add_data_in_row(T first, T last) {
13 // Iterate over the range and add each element to the content
14 for (; first != last;) {
15 buffer_ << *first;
16 if (++first != last) buffer_ << delimiter_;
17 }
18 buffer_ << std::endl;
19 }
20
21 bool save_to_file(std::string path) {
22 std::fstream file;
23 // Open the file in truncate mode
24 file.open(path, std::ios::out | std::ios::trunc);
25
26 if (!file.good()) {
27 return false;
28 }
29
30 file << buffer_.str();
31 file.close();
32
33 return true;
34 }
35
36 std::string get_content() { return buffer_.str(); }
37
38 private:
39 std::ostringstream buffer_;
40 std::string delimiter_;
41};
Definition: csv_writer.h:7
bool save_to_file(std::string path)
Definition: csv_writer.h:21
std::string get_content()
Definition: csv_writer.h:36
CSVWriter(std::string delimiter=",")
Definition: csv_writer.h:9
void add_data_in_row(T first, T last)
Definition: csv_writer.h:12