简体   繁体   中英

Segmentation Fault 11 while trying to run compiled C++ code

I am doing an assignment (.csv-parser) for uni. While trying to run the code after compilation it returns a SegFault 11.

This is my code:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

int main (int argc, char** argv) {

string line;
string buffer;

vector<vector<string> > database;
vector<string> dataset;

//bool start = true;

ifstream denkmaeler;
denkmaeler.open(argv[1]);
ifstream denkmal;
denkmal.open(argv[1]);

int semic = 1;
//int semic2 = 1;
int zaehler = 0;

if (denkmal.is_open()){
    (getline(denkmal, buffer));

    for (int i = 0; i < buffer.length(); i++ ){
        if(buffer[i] == ';'){
            semic++;}

    }
}

denkmal.close();

if(denkmaeler.is_open()) {
    //if (counter < 1) {
    while (getline(denkmaeler, buffer));
    if (line.back() == *argv[2]) {
        line += argv[2];
        stringstream ss(line);
        while (getline(ss, line, *argv[2])) {
            dataset.push_back(line);
            database.push_back(dataset);
            dataset.clear();

        }
    }


}

for (int x=0, y=semic; x < semic; y=database.size(), x++, y++){
    if (x > semic){
        x=0;
        cout << '\n' << "-------------------------------" << '\n' << endl;

    }
    if (database[y][0].length() == 0){
        database[y][0] = "not available";
    }

    cout << database[x][0] << " : "  << database[y][0] << endl;
}
}

If someone would be able to point out my mistake I would be very thankful. I read some posts pointing out that the problem could be an array but I am not sure how that could be.

For segmentation error, it is best to use a debugging tool which can exactly show you where the error is (which line)!

I am using gdb, i recommend you to google it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM