-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_parser.cpp
More file actions
43 lines (34 loc) · 816 Bytes
/
csv_parser.cpp
File metadata and controls
43 lines (34 loc) · 816 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
35
36
37
38
39
40
41
42
43
#include "csv_parser.h"
csv_parser::csv_parser() {
}
csv_parser::~csv_parser() {
if (m_if.is_open())
m_if.close();
if (m_of.is_open())
m_of.close();
}
int csv_parser::open(std::string &ifilename, std::string &ofilename) {
if (!ofilename.empty())
m_of.open(ofilename.c_str(),std::ios::out);
else
m_of.open("output.csv",std::ios::out);
m_if.open(ifilename.c_str(),std::ios::in);
if (!m_if.is_open())
return -1;
return 0;
}
std::vector<std::string> csv_parser::parse() {
std::vector<std::string> column;
std::string row;
while (std::getline(m_if, row)) {
column.push_back(row);
row.clear();
}
return column;
}
void csv_parser::write(std::vector<std::string> data) {
std::vector<std::string>::iterator it;
for (it = data.begin(); it != data.end(); it++) {
m_of << *it << "\n";
}
}