Fix Line-Reading caused by \r whitespace

This commit is contained in:
Matti 2024-11-12 11:17:05 +01:00
parent 9b1c0ef213
commit 01292040fe
2 changed files with 33 additions and 20 deletions

View File

@ -15,17 +15,6 @@
// ucr_ncic_code, // ucr_ncic_code,
// latitude, // latitude,
// longitude, // longitude,
typedef struct CRIME{
std::string cdatetime;
std::string address;
std::string district;
std::string beat;
std::string grid;
std::string crimedescription;
std::string ucr_ncic_code;
std::string latitude;
std::string longitude;
} Crime;
void split(const std::string& s, char c,std::vector<std::string>& v) { void split(const std::string& s, char c,std::vector<std::string>& v) {
std::string::size_type i = 0; std::string::size_type i = 0;
@ -41,9 +30,9 @@ void split(const std::string& s, char c,std::vector<std::string>& v) {
} }
} }
std::string readFile(std::string &fileName) { std::string readFile(std::string &fileName, std::vector<Crime*> &allCrimes) {
std::string content; std::string content;
std::ifstream infile(fileName); std::ifstream infile;
infile.open(fileName); infile.open(fileName);
if (!infile.is_open()) { if (!infile.is_open()) {
@ -54,8 +43,14 @@ std::string readFile(std::string &fileName) {
std::string line; std::string line;
while (std::getline(infile, line)) { while (std::getline(infile, line)) {
std::cout << line << std::endl;
content += line; content += line;
std::vector<std::string> actualLines;
split(line, '\r', actualLines);
for (auto actual_line: actualLines) {
allCrimes.push_back(stringToCrime(actual_line));
}
} }
return content; return content;
} }
@ -66,7 +61,7 @@ Crime* stringToCrime(std::string &input) {
Crime* thisCrime = new Crime; Crime* thisCrime = new Crime;
for (int i = 0; i < tokens.size(); i++) { for (int i = 0; i < tokens.size(); i++) {
switch (i) { switch (i%tokens.size()) {
case 0: case 0:
thisCrime->cdatetime = tokens[0]; thisCrime->cdatetime = tokens[0];
break; break;
@ -93,17 +88,21 @@ Crime* stringToCrime(std::string &input) {
break; break;
case 8: case 8:
thisCrime->longitude = tokens[8]; thisCrime->longitude = tokens[8];
break;
default: default:
std::cout << "I am the Default case " << tokens.size() << std::endl;
} }
} }
return thisCrime; return thisCrime;
} }
void Aufg4Main() { void Aufg4Main() {
// std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv"; std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
std::string fileName = "../Aufg4/IO-Files/debugging.test"; std::vector<Crime*> allCrimes;
std::string content = readFile(fileName); std::string content = readFile(fileName, allCrimes);
std::vector<Crime> vector;
// std::cout << content << std::endl << "EndOfFile" << std::endl;
std::cout << allCrimes.at(2)->crimedescription << std::endl;
} }

View File

@ -3,11 +3,25 @@
#include <string> #include <string>
#include <vector> #include <vector>
typedef struct CRIME{
std::string cdatetime;
std::string address;
std::string district;
std::string beat;
std::string grid;
std::string crimedescription;
std::string ucr_ncic_code;
std::string latitude;
std::string longitude;
} Crime;
void split(const std::string& s, char c,std::vector<std::string>& v); void split(const std::string& s, char c,std::vector<std::string>& v);
void Aufg4Main(); void Aufg4Main();
std::string readFile(std::string &filename); std::string readFile(std::string &filename, std::vector<Crime*> &allCrimes);
Crime* stringToCrime(std::string &input);
#endif //CRIMESTATS_H #endif //CRIMESTATS_H