Good evening, everyone! Next, I made this code from a response here from the stack overflow and it is giving segmentation fault 11 in the last line of the file, somebody help me to solve, please! This is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
void read_archive(const char *in) {
ifstream file(in);
string buffer;
vector <vector<string> > linhas; //Vetor de vetor para todas as linhas
while(!file.eof()) {
getline(file, buffer);
stringstream ss(buffer);
vector<string> linha;
while(getline(ss, buffer, ';')) {
linha.push_back(buffer);
}
linhas.push_back(linha);
}
vector<unsigned long> ColunasRelevantes; //ColunasRelevantes[X];
for(size_t i = 0; i < linhas[0].size(); i++) {
string nomeColunas = linhas[0][i].substr(1, linhas[0][i].size() - 2); //Tirar as 2 aspas
if(nomeColunas == "SG_PARTIDO") {
ColunasRelevantes.push_back(i);
}
}
for(size_t i = 0; i < linhas.size(); i++) {
for(size_t j = 0; j < ColunasRelevantes.size(); j++) {
unsigned long Coluna = ColunasRelevantes[j];
string texto = linhas[i][Coluna].substr(1, linhas[i][Coluna].size() - 2);
cout << texto << "\t";
}
cout << endl;
}
}
int main () {
read_archive("consulta_cand_2018_BR.csv");
return 0;
}