Program crashes after opening file [closed]

不问归期 提交于 2019-12-01 18:54:54

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).

sjs

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!