forked from hlamotte/decision-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVReader.cpp
30 lines (24 loc) · 1.05 KB
/
CSVReader.cpp
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
#include <stdexcept>
#include "CSVReader.hpp"
DataFrame CSVReader::read(std::string filePath) {
std::string line; /* string to hold each line */
DataFrame array; /* vector of vector<int> for 2d array */
std::ifstream f (filePath); /* open file */
try {
if (!f.is_open()) { /* validate file open for reading */
throw (filePath);
};
} catch (char* filePath) {
throw std::invalid_argument(("error while opening file " + std::string(filePath)).c_str());
}
while (getline (f, line)) { /* read each line */
std::string val; /* string to hold value */
std::vector<int> row; /* vector for row of values */
std::stringstream s (line); /* stringstream to parse csv */
while (getline (s, val, ',')) /* for each value */
row.push_back (stoi(val)); /* convert to int, add to row */
array.push_back (row); /* add row to array */
}
f.close();
return array;
}