简体   繁体   中英

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. I have tried different debuggiing tactics of adding cout's to see which point the code is getting screwed up to no avail. 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 .

Especially with segfaults using debuggers is far better than doing print debugging.

I suggest looking for tutorials on gdb . Another option is trying to compile the code with address sanitization (just google for 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