简体   繁体   English

尝试运行已编译的 C++ 代码时出现分段错误 11

[英]Segmentation Fault 11 while trying to run compiled C++ code

I am doing an assignment (.csv-parser) for uni.我正在为 uni 做一个作业 (.csv-parser)。 While trying to run the code after compilation it returns a SegFault 11.在编译后尝试运行代码时,它返回 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我正在使用gdb,我建议你谷歌一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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