Program ulega awarii po otwarciu pliku [zamknięty]
Muszę odczytać wartości z pliku do mojego programu. Plik otwiera się pomyślnie, ale od razu ulega awarii. Czy coś jest nie tak z moim kodem?
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;
}
Dzięki.
Edytować:
Po sprawdzeniu mam problem zaraz po tym
else
{
lastInt->nextNode = anotherInt;
}
Musi więc wystąpić problem z tym kodem:
lastInt = lastInt->nextNode;
lastInt->intValue = fileInt;
lastInt->nextNode = NULL;
intInputFile >> fileInt;
Ponieważ miałem bezpośrednio po nim instrukcję cout, która nie działała.
Po dokładniejszym przyjrzeniu się problem dotyczy tej linii:
intInputFile >> fileInt;