-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcsvreader.cpp
More file actions
48 lines (37 loc) · 1.29 KB
/
csvreader.cpp
File metadata and controls
48 lines (37 loc) · 1.29 KB
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
44
45
46
47
48
#include "csvreader.hpp"
csvReader::csvReader(std::string filename)
{
file = std::ifstream(filename);
}
csvReader::~csvReader()
{
file.close();
}
// read() method reads from file and returns vector of lines (vectors of strings that are made from lines by spliting on ",").
std::vector<std::vector<std::string>> csvReader::read() {
std::vector<std::string> rows;
std::vector<std::vector<std::string>> splitRows;
if(!file.is_open()) {
std::cerr << "Invalid path to file" << std::endl;
return splitRows;
}
unsigned long n = 0;
std::string line;
//Getting all lines, and puting them in rows.
while(getline(file, line))
{
rows.push_back(line);
n++;
}
//Spliting csv data and triming results. Puting results in splitRows.
for(unsigned i = 0; i < rows.size(); i++) {
std::vector<std::string> tmp;
//Split
boost::split(tmp, rows[i], boost::is_any_of(","));
//Trim
std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](std::string x){return x.erase(x.find_last_not_of(" \n\r\t")+1);});
std::transform(tmp.begin(), tmp.end(), tmp.begin(), [](std::string x){return x.erase(0, x.find_first_not_of(" \n\r\t"));});
splitRows.push_back(tmp);
}
return splitRows;
}