[英]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.