【发布时间】:2012-12-19 00:56:44
【问题描述】:
我需要将文件中的值读入我的程序。该文件已成功打开,但随后立即崩溃。我的代码有问题吗?
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;
【问题讨论】:
-
你能添加更多 cout 来找到代码崩溃的确切位置吗?
-
如果它崩溃了,那么是的,你的程序有问题。
-
好的,我现在就试试。
-
你现在要尝试什么?
-
@ScottHunter Josay 推荐了什么。