I'm sing C++ to build a csv processor for study purposes and having some throuble with segmentation faults. This processor will have one main class (csvcontroller) that should be the instance of the current file. This class controls two structures. One of them (CsvLine) is a list of all file lines and the other one (CsvColumn) represents a list of all columns on each list. My segmentation fault happens when i try to write the column value into the struct but it only happens when i'm using strings. If we replace string for int, the processor runs the way i want.
This is my code:
csvcontroller.h:
#ifndef CSVCONTROLLER_H
#define CSVCONTROLLER_H
#include <iostream>
#include <string>
using namespace std;
typedef struct CsvLine{
struct CsvLine* next = NULL;
struct CsvColumn* first = NULL;
struct CsvColumn* last = NULL;
struct CsvLine* previous = NULL;
};
typedef struct CsvColumn{
struct CsvColumn* previous = NULL;
string value = NULL;
struct CsvColumn* next = NULL;
};
class csvcontroller{
public:
bool loadfile(string file_location,char separator);
bool savefile(string file_location,char separator);
CsvLine* getFirst();
CsvLine* getLast();
CsvLine* addLine();
void addColumn(CsvLine* line,string* value);
int getCount();
protected:
string path;
CsvLine* first = NULL;
CsvLine* last = NULL;
int line_count;
private:
};
#endif // CSVCONTROLLER_H
csvcontroller.cpp
#include "csvcontroller.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;
bool csvcontroller::loadfile(string file_location,char separator){
this->path = file_location;
const char * path = this->path.c_str();
//Initializing read stream
ifstream file;
file.open(path);
//Reading our file
while(!file.eof()){
string line;
std::getline(file,line);
stringstream stream(line);
if(!line.empty()){
string col;
CsvLine* ln = this->addLine();
while(std::getline(stream,col,separator)){
this->addColumn(ln,&col);
}
}//if(!line.empty
}//while(!file.eof())
return false;
}
CsvLine* csvcontroller::addLine(){
CsvLine* item = (CsvLine*) malloc(sizeof(CsvLine));
if(this->first == NULL){
this->first = item;
this->last = item;
}else{
this->last->next = item;
item->previous = this->last;
this->last = item;
}
}
void csvcontroller::addColumn(CsvLine* line,string* value){
CsvColumn* col = (CsvColumn*) malloc(sizeof(CsvColumn));
col->value = value;//Here's the problem
if(line->first == NULL){
line->first = col;
line->last = col;
}else{
line->last->next = col;
col->previous = line->last;
line->last = col;
}
}
/*
* GET & SET
*/
CsvLine* csvcontroller::getFirst(){
return this->first;
}
CsvLine* csvcontroller::getLast(){
return this->last;
}
main.cpp:
#include <iostream>
#include <csvcontroller.h>
using namespace std;
int main(){
csvcontroller lista_logradouros;
lista_logradouros.loadfile("C:\\Users\\d786282\\Desktop\\Base_JUN_V2\\20140828_base_jun_padronizada.csv",';');
CsvLine* aux = lista_logradouros.getFirst();
do{
aux = aux->next;
}while(aux != NULL);
}
How can i debug / solve this?