-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvParser.cpp
42 lines (36 loc) · 1.1 KB
/
csvParser.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
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
std::vector< std::vector<float> > parseCsv(std::string fileName);
void printMatrix(std::vector< std::vector<float> > matrix);
int main() {
std::string fileName = "diabetes.csv";
std::vector< std::vector<float> > csvMatrix = parseCsv(fileName);
printMatrix(csvMatrix);
return 0;
}
std::vector< std::vector<float> > parseCsv(std::string fileName) {
std::ifstream inputStream(fileName);
std::vector< std::vector<float> > matrix;
std::string line;
while(std::getline(inputStream,line)) {
std::stringstream lineStream(line);
std::string cell;
std::vector<float> row;
while(std::getline(lineStream,cell,',')) {
row.push_back(std::stof(cell));
}
matrix.push_back(row);
}
return matrix;
}
void printMatrix(std::vector< std::vector<float> > matrix) {
for(int i=0; i<matrix.size(); i++) {
for(int j=0; j<matrix[i].size(); j++) {
std::cout << matrix[i][j] << ' ';
}
std::cout << std::endl;
}
}