我需要將文件中的值讀入我的程序。該文件已成功打開,但之後立即崩潰。我的代碼有問題嗎?打開文件後程序崩潰
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;
}
所以必須有與此代碼的一個問題一個問題:因爲我直接了COUT聲明
lastInt = lastInt->nextNode;
lastInt->intValue = fileInt;
lastInt->nextNode = NULL;
intInputFile >> fileInt;
之後它並沒有工作。
而且尋找到它經過,問題是這一行:
intInputFile >> fileInt;
你可以添加更多的cout來找到代碼崩潰的確切位置嗎? – Josay
如果它崩潰了,那麼是的,你的程序有問題。 –
好的,我現在就試試。 –