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.