簡體   English   中英

Seg 故障問題 C++(文件 IO/getline)

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

我有一個與接收分段錯誤有關的問題,該錯誤可能來自我的代碼的這 3 個部分之一中的某個部分。 我嘗試了添加 cout 的不同調試策略,以查看代碼在哪一點被搞砸了,但無濟於事。 任何和所有幫助將不勝感激。

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

這些函數存在於單獨的文件中,但按照我粘貼它們的順序進行交互。

您的代碼無法編譯,因為它既不是最小的示例,也不是完整的代碼集。

沒有深入分析,這條線似乎很可疑:

if (tempAvailable = 0);

它根本不應該編譯,而且您還為tempAvailable分配了 0 。

尤其是使用調試器的段錯誤比使用打印調試要好得多。

我建議尋找關於gdb教程。 另一種選擇是嘗試使用地址清理來編譯代碼(只需 google 即可)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM