ファイルからプログラムに値を読み込む必要があります。ファイルは正常に開いていますが、すぐにクラッシュします。私のコードに何か問題がありますか?
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;