-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileProcessor.cpp
132 lines (112 loc) · 3.77 KB
/
FileProcessor.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
127
128
129
130
131
132
/*
* File: FileProcessor.cpp
* Author: heshan
*
* Created on April 14, 2018, 7:45 PM
*/
#include <vector>
#include <exception>
#include <sstream>
#include "FileProcessor.h"
FileProcessor::FileProcessor() { }
FileProcessor::FileProcessor(const FileProcessor& orig) { }
FileProcessor::~FileProcessor() { }
std::vector<double> FileProcessor::read(std::string fileName, int valuesPerLine) {
std::vector<double> values;
std::string line;
std::ifstream file (fileName);
int lineNo = 0;
if (file.is_open()) {
if (valuesPerLine == 1) {
while ( getline (file,line) ) {
lineNo++;
try{
values.push_back(std::stod(line));
} catch (std::exception& e) {
std::cout<<std::endl<<"Error in line "<<lineNo<<": "<<e.what()<<std::endl;
}
}
}
file.close();
}
else std::cout << "Unable to open file '"<<fileName<<"'";
return values;
}
std::vector<double> * FileProcessor::readMultivariate(std::string fileName, int lines, int variables, int * inputCols, int targetValCol) {
std::string line;
std::ifstream file (fileName);
std::string token;
int tokenNo = 0;
int lineNo = 0;
std::vector<double> target;
std::vector<double> * data;
data = new std::vector<double>[lines+1];
if (file.is_open()) {
while ( getline (file,line) ) {
std::vector<double> input;
lineNo++;
try{
std::stringstream ss(line);
tokenNo = 0;
while(std::getline(ss, token, ',')) {
if (tokenNo == targetValCol) {
target.push_back(std::stod(token));
} else if (inputCols[tokenNo] == 1) {
input.push_back(std::stod(token));
}
tokenNo++;
}
data[lineNo-1] = input;
} catch (std::exception& e) {
std::vector<double> input (variables-1,0.0);
data[lineNo-1] = input;
target.push_back(0.0);
std::cout<<std::endl<<"Error in line "<<lineNo<<": "<<e.what()<<std::endl;
}
if (lineNo == lines) break;
}
file.close();
data[lines] = target;
}
else std::cout << "Unable to open file '"<<fileName<<"'";
return data;
}
int FileProcessor::write(std::string fileName) {
out_file.open(fileName,std::ofstream::out | std::ofstream::app);
return 0;
}
int FileProcessor::append(std::string line) {
out_file<<"";
return 0;
}
int FileProcessor::writeUniVariate(std::string fileName, std::string outFileName, int valuesPerLine, int columnIndx) {
std::string line;
std::ifstream file (fileName);
std::string token;
int tokenNo = 0;
int lineNo = 0;
std::ofstream out_file;
out_file.open(outFileName,std::ofstream::out | std::ofstream::trunc);
if (file.is_open()) {
if (valuesPerLine > 1) {
while ( getline (file,line) ) {
lineNo++;
try{
std::stringstream ss(line);
tokenNo = 0;
while(std::getline(ss, token, ',')) {
if (tokenNo == columnIndx) {
out_file<<token<<"\n";
}
tokenNo++;
}
} catch (std::exception& e) {
std::cout<<std::endl<<"Error in line "<<lineNo<<": "<<e.what()<<std::endl;
}
}
}
file.close();
}
else std::cout << "Unable to open file '"<<fileName<<"'";
return 0;
}