我需要将文件中的值读入我的程序。该文件已成功打开,但随后立即崩溃。我的代码有问题吗?
void createList(intNode*& intList)
{
intNode* lastInt; //points to last integer in file
lastInt = NULL;
int fileInt; //int read from input file
ifstream intInputFile;
intInputFile.open("intInput.txt");
if (intInputFile.is_open())
{
cout << "intInput.txt open successful" << endl;
}
else
{
cout << "intInput.txt open unsuccessful" << endl;
}
intInputFile >> fileInt;
while(!intInputFile.eof())
{
intNode* anotherInt;
anotherInt = new intNode;
if(intList==NULL)
{
intList = anotherInt;
lastInt = anotherInt;
}
else
{
lastInt->nextNode = anotherInt;
}
lastInt = lastInt->nextNode;
lastInt->intValue = fileInt;
lastInt->nextNode = NULL;
intInputFile >> fileInt;
}
intInputFile.close();
cout << "List created from input file" << endl;
}
谢谢。
编辑:
检查后,我马上就有问题
else
{
lastInt->nextNode = anotherInt;
}
所以这段代码肯定有问题:
lastInt = lastInt->nextNode;
lastInt->intValue = fileInt;
lastInt->nextNode = NULL;
intInputFile >> fileInt;
因为我在它之后直接有一个 cout 声明,但它没有用。
在进一步研究之后,问题出在这一行:
intInputFile >> fileInt;