forked from Yiqing-Gu/Pidentify
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSVWrite.hpp
More file actions
34 lines (30 loc) · 859 Bytes
/
CSVWrite.hpp
File metadata and controls
34 lines (30 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef CSVWrite_HPP
#define CSVWrite_HPP
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>
template <typename T> void writeRow(const std::vector<T>& row, std::ofstream& out) {
std::copy(row.begin(), row.end() - 1, std::ostream_iterator<T>(out, ","));
out << *(row.end() - 1) << "\n";
}
// Add rows to CSV
template <typename T> void writeToCSV(const std::vector<std::string>& header, const std::vector<std::vector<T> >& rows,
const std::string& filename) {
std::ofstream out;
if (header.size() > 0) {
out.open(filename);
// Add a header if this is the first time writing to the CSV
writeRow<std::string>(header, out);
}
else {
out.open(filename, std::ios::app);
}
size_t totalRows = rows.size();
for (size_t i = 0; i < totalRows; ++i) {
writeRow<T>(rows[i], out);
}
out.close();
}
#endif