-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV.cpp
126 lines (117 loc) · 3.47 KB
/
CSV.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "CSV.h"
using namespace std;
// read the .CSV file and the output is a 2D vector of int
void CSV::readCSV(std::istream &input, std::vector< std::vector<int> > &output)
{
std::string csvLine;
// read every line from the stream
while (std::getline(input, csvLine))
{
std::istringstream csvStream(csvLine);
std::vector<int> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while (std::getline(csvStream, csvElement, ';')) // you can change this delimiter ";"
{
csvColumn.push_back(stoi(csvElement));
}
output.push_back(csvColumn);
}
}
// read the .CSV file and the output is 2D of double vector
void CSV::readCSV(std::istream &input, std::vector< std::vector<double> > &output)
{
std::string csvLine;
// read every line from the stream
while (std::getline(input, csvLine))
{
std::istringstream csvStream(csvLine);
std::vector<double> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while (std::getline(csvStream, csvElement, ';'))
{
csvColumn.push_back(stod(csvElement));
}
output.push_back(csvColumn);
}
}
// read the .CSV file and the output is 2D of float vector
void CSV::readCSV(std::istream &input, std::vector< std::vector<float> > &output)
{
std::string csvLine;
// read every line from the stream
while (std::getline(input, csvLine))
{
std::istringstream csvStream(csvLine);
std::vector<float> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while (std::getline(csvStream, csvElement, ';'))
{
csvColumn.push_back(stof(csvElement));
}
output.push_back(csvColumn);
}
}
// read the .CSV file and the output is 2D of string vector
void CSV::readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
std::string csvLine;
// read every line from the stream
while (std::getline(input, csvLine))
{
std::istringstream csvStream(csvLine);
std::vector<std::string> csvColumn;
std::string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while (std::getline(csvStream, csvElement, ';'))
{
csvColumn.push_back(csvElement);
}
output.push_back(csvColumn);
}
}
// read the .CSV file and the output is 1D of double vector
void CSV::readCSV(std::istream &input, std::vector<double> &output)
{
std::string csvElement;
while (std::getline(input, csvElement, ';'))
{
output.push_back(stod(csvElement));
}
}
// read the .CSV file and the output is a double
void CSV::readCSV(std::istream &input, double &output)
{
std::string csvElement;
while (std::getline(input, csvElement, ';'))
{
output = stod(csvElement);
}
}
//write A 1D float vector to .CSV file
void CSV::writeCSV(std::string fileName, std::vector<float> inputChar) {
ofstream input;
input.open(fileName, std::ios_base::app);
for (int i = 0; i < inputChar.size(); i++) {
input << inputChar[i];
input << ";";
}
input << "\n";
}
//write an integer to .CSV file
void CSV::writeCSV(std::string fileName) {
ofstream input;
input.open(fileName, std::ios_base::app);
input << "\n";
}