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,
// latitude,
// 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) {
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::ifstream infile(fileName);
std::ifstream infile;
infile.open(fileName);
if (!infile.is_open()) {
@ -54,8 +43,14 @@ std::string readFile(std::string &fileName) {
std::string line;
while (std::getline(infile, line)) {
std::cout << line << std::endl;
content += line;
std::vector<std::string> actualLines;
split(line, '\r', actualLines);
for (auto actual_line: actualLines) {
allCrimes.push_back(stringToCrime(actual_line));
}
}
return content;
}
@ -66,7 +61,7 @@ Crime* stringToCrime(std::string &input) {
Crime* thisCrime = new Crime;
for (int i = 0; i < tokens.size(); i++) {
switch (i) {
switch (i%tokens.size()) {
case 0:
thisCrime->cdatetime = tokens[0];
break;
@ -93,17 +88,21 @@ Crime* stringToCrime(std::string &input) {
break;
case 8:
thisCrime->longitude = tokens[8];
break;
default:
std::cout << "I am the Default case " << tokens.size() << std::endl;
}
}
return thisCrime;
}
void Aufg4Main() {
// std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
std::string fileName = "../Aufg4/IO-Files/debugging.test";
std::string content = readFile(fileName);
std::vector<Crime> vector;
std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
std::vector<Crime*> allCrimes;
std::string content = readFile(fileName, allCrimes);
// 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 <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 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