简体   繁体   English

Seg 故障问题 C++(文件 IO/getline)

[英]Seg Fault Issue C++ (file IO / getline)

I am having an issue pertaining to receiving a segmentation fault which is coming from somewhere within one of these 3 portions of my code presumably.我有一个与接收分段错误有关的问题,该错误可能来自我的代码的这 3 个部分之一中的某个部分。 I have tried different debuggiing tactics of adding cout's to see which point the code is getting screwed up to no avail.我尝试了添加 cout 的不同调试策略,以查看代码在哪一点被搞砸了,但无济于事。 Any and all help will greatly be appreciated.任何和所有帮助将不胜感激。

void agency::readAllData()
{
    int index;
    char inputFile[100];
    char delim = '}';
    car * p_car;
    p_car = m_inventory;
    cout << "Input file name: " << endl ;
    cin >> inputFile;
    ifstream input (inputFile);
    if (input)
    {
        input >> m_name >> m_zipcode;
        for (index = 0; index < 5; index++)
        {
            int tempYear, tempAvailable;
            float tempPrice;
            char tempMake[256], tempModel[256], tempSensors[256], tempOwner[256];
            input >> tempYear >> tempMake >> tempModel >> tempPrice;
            input.getline(tempSensors, 256, delim);
            input >> tempAvailable;
            if (tempAvailable = 0);
            {
                input >> tempOwner;
            }
            p_car -> readCars(tempYear, tempMake, tempModel, tempPrice, tempSensors, tempAvailable, tempOwner);
            p_car++;
        }
    }
    else
    {
        cerr << "Input file cannot be opened" << endl;
        return;
    }
    return;
}

void sensor::readSensors(char tempSensors[], sensor *sensor_ptr)
{
    int index1;
    int index2 = 0;
    int index3 = 0;
    for(index1 = 0; tempSensors[index2] != '\0'; index1++)
    {
        if (tempSensors[index1] = 'g')
        {
            gpsCount++;
        }
        else if (tempSensors[index1] = 'l')
        {
            lidarCount++;
        }
        else if (tempSensors[index1] = 'r')
        {
            radarCount++;
        }
        else if (tempSensors[index1] = 'c')
        {
            cameraCount++;
        }
        for(index2+=index1; tempSensors[index2] != ' '; index2++)
        {
            sensor_ptr->m_type[index3] = tempSensors[index2];
            index3++;
        }
        sensor_ptr++;
    }
    return;
}

these functions exist among seperate files but interact in the order I have pasted them.这些函数存在于单独的文件中,但按照我粘贴它们的顺序进行交互。

Your code does not compile, since it's neither a minimal example nor the complete set of code.您的代码无法编译,因为它既不是最小的示例,也不是完整的代码集。

Without analyzing it deeply, this line seems fishy:没有深入分析,这条线似乎很可疑:

if (tempAvailable = 0);

It shouldn't compile at all and also you are assigning 0 to tempAvailable .它根本不应该编译,而且您还为tempAvailable分配了 0 。

Especially with segfaults using debuggers is far better than doing print debugging.尤其是使用调试器的段错误比使用打印调试要好得多。

I suggest looking for tutorials on gdb .我建议寻找关于gdb教程。 Another option is trying to compile the code with address sanitization (just google for it).另一种选择是尝试使用地址清理来编译代码(只需 google 即可)。

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

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