Programm stürzt nach dem Öffnen der Datei ab [geschlossen]
Ich muss Werte aus einer Datei in mein Programm einlesen. Die Datei wird erfolgreich geöffnet, stürzt dann jedoch sofort ab. Stimmt etwas mit meinem Code nicht?
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;
}
Vielen Dank.
Bearbeiten:
Nach der Überprüfung habe ich sofort ein Problem
else
{
lastInt->nextNode = anotherInt;
}
Es muss also ein Problem mit diesem Code geben:
lastInt = lastInt->nextNode;
lastInt->intValue = fileInt;
lastInt->nextNode = NULL;
intInputFile >> fileInt;
Weil ich eine Cout-Anweisung direkt danach hatte und es nicht funktionierte.
Und nachdem wir uns näher damit befasst haben, liegt das Problem bei dieser Zeile:
intInputFile >> fileInt;