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

标签 c++ compiler-errors segmentation-fault

我正在为 uni 做一个作业(.csv-parser)。在编译后尝试运行代码时,它会返回 SegFault 11。

这是我的代码:

#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;
}
}

如果有人能够指出我的错误,我将非常感激。我读了一些帖子,指出问题可能是一个数组,但我不确定那是怎么回事。

最佳答案

对于段错误,最好使用可以准确显示错误在哪里(哪一行)的调试工具!

我正在使用gdb,我建议你谷歌它

关于c++ - 尝试运行已编译的 C++ 代码时出现段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55107409/

相关文章:

java - 字符串和变量串联的JOptionPane错误

sass - 不兼容的单位 : 'rem' and 'px' - Bootstrap 4. 3.1

c++ - 为什么节点不能链接在一起?

c - 访问作为 void 指针段错误传递的 int 数组

python - 为什么这个 Python 代码在 Ubuntu 上会出现段错误,但在 Windows 上却没有?

c++ - Runge-Kutta 四阶粒子平流代码示例

c++ - C++ 模板的编译器错误表明这不是结构体的成员

c++ - 用时间检查数据?

c++ - FFmpeg编译opus错误

c++ - 如何将成员函数传递给另一个成员函数中使用的函数?