4

I need to read values from a file into my program. The file is opening successfully, but then it crashes right away. Is there something wrong with my code?

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;
}

Thanks.

Edit:

After checking, I have a problem right after

else
    {
        lastInt->nextNode = anotherInt;
    }

So there must be a problem with this code:

    lastInt = lastInt->nextNode;
    lastInt->intValue = fileInt;
    lastInt->nextNode = NULL;
    intInputFile >> fileInt;

Because I had a cout statement directly after it and it didn't work.

And after looking into it more, the problem is with this line:

     intInputFile >> fileInt;
A A
  • 197
  • 7

2 Answers2

3

Assuming intList isn't NULL, then you'll call lastInt->nextNode = anotherInt; during your first iteration of the loop while lastInt is still NULL causing the program to crash (due to it following a null pointer).

Mario
  • 35,726
  • 5
  • 62
  • 78
  • My program is crashing due because of the statement: intInputFile >>fileInt; – A A Dec 19 '12 at 01:20
  • That doesn't match your edited post "I have a problem right after". You should edit your question. Also try to set a break point at that line and verify your stream is fine when reaching it. – Mario Dec 19 '12 at 01:23
  • Okay I did. I know everything is okay beforehand, I think the problem is from my original file of values. I made a file with 5 integers, and when my program tries to read the next integer it crashes. I put each integer value in my file on a separate line, is that okay? – A A Dec 19 '12 at 01:32
  • Not sure I understood you right, but this should work anyway: If the input isn't valid, the value would be set to `0` and the program would still continue - it wouldn't crash. There's most likely something wrong with the stream. – Mario Dec 19 '12 at 01:38
  • Oh okay, I'm new to this, so what do you mean by something wrong with the stream? – A A Dec 19 '12 at 01:41
  • I have changed the area you spoke of:else { lastInt->nextNode = new intNode; lastInt = lastInt->nextNode; lastInt->nextNode = NULL; } – A A Dec 19 '12 at 02:20
  • But now my program runs uncontrollably... – A A Dec 19 '12 at 02:20
  • "Wrong" as in "you somehow destroyed or screwed up a part of the `istream` object. It shouldn't crash by itself, even if the data is wrong. Your new code won't fix the problem, `lastInt` is still undefined/`NULL` at the first iteration. – Mario Dec 19 '12 at 10:26
1

Assuming that the intInput.txt file is formatted properly, your intInputFile >> fileInt; line should read the first integer from it just fine, so there must be some problem with the ifstream. The is_open member function of ifstream only tells you whether the stream has a file associated with it. It doesn't necessarily tell you if there was a problem opening the file. You can check for that with the good function. E.g.:

if (intInputFile.good()) 
    cout << "intInputFile is good" << endl;
else 
    cout << "intInputFile is not good" << endl;

Depending on your system, you may be able to find out the cause of any error using strerror(errno) as follows:

#include <cstring>
#include <cerrno>

...

if (!intInputFile.good())
    cout << strerror(errno) << endl;

This works for me, but see this question for more information as it may not work everywhere.

Community
  • 1
  • 1
sjs
  • 8,830
  • 3
  • 19
  • 19