所以我试图读这个文件。一切看起来都应该起作用,但在运行期间程序超时并停止工作,我必须关闭它。到底是怎么回事?我怀疑oef()测试永远不会返回true,它会一直在文件中寻找更多的东西。我没有拖动文本文件中的空行。我试过这个疯狂的调试。我找不到任何错误,但仍然拒绝工作。C++ eof()问题 - 永远不会返回true?
Pet** petArray;
ifstream textFile2;
textFile2.open("pets.txt");
int i = 0;
string temp;
int tmpNum = 0;
if (textFile2.is_open())
{
while (!textFile2.eof())
{
getline(textFile2, temp);
petArray = new Pet*[arraySize];
if (temp == "Dogs" || temp == "Cats" || temp == "Iguanas" || temp == "Pigs")
{
if (temp == "Dogs") tmpNum = 0;
if (temp == "Cats") tmpNum = 1;
if (temp == "Iguanas") tmpNum = 2;
if (temp == "Pigs") tmpNum = 3;
temp == "";
}
else
{
if (tmpNum == 0)
{
petArray[i] = new Dog(temp);
cout << "Dog " << temp << " added" << endl;
}
if (tmpNum == 1)
{
petArray[i] = new Cat(temp);
cout << "Cat " << temp << " added" << endl;
}
if (tmpNum == 2)
{
petArray[i] = new Iguana(temp);
cout << "Iguana " << temp << " added" << endl;
}
if (tmpNum == 3)
{
petArray[i] = new Pig(temp);
cout << "Pig " << temp << " added" << endl;
}
arraySize++;
}
i++;
}
}
这里是文本文件的格式:
Dogs
d1
d2
Cats
c1
c2
Iguanas
i1
i2
Pigs
p1
p2
有什么建议?
是的,我知道它不会解决问题,但没有足够的信息来分析。温度的定义?如果你在调试器中断,它停在哪里? – ybungalobill 2010-10-01 18:31:57
我可以改为:while(getline(textFile2,temp)?另外:我增加了额外的缺失内容 – NateTheGreatt 2010-10-01 18:33:46
是的,你可以。你的程序在你的计算机上暂停,你怎么确定它永远不会从循环中返回? – ybungalobill 2010-10-01 18:45:45