Search code examples
c++fileloopsifstream

C++ Making a function for reading in a .txt file and checking to make sure the file exists, but my loop isn't working


I'm trying to make a function to read in a file, check to make sure it exists (and if it doesn't ask for the filename again), and then print "Loading..." if it DOES find the file.

However, the loop only goes through once. It prints out "FILE ERROR: File does not exist!" and then ends the program. Any ideas? Tell me if you need more information!

void find_board(string fileName)
{
bool filefound = 0;
ifstream in_stream;

do
{
    cout << "Enter the name of the file containing the board: ";
    cin >> fileName;

    in_stream.open(fileName.c_str());

    if (in_stream.fail())
    {
        filefound = 0;
        cout << "FILE ERROR: File does not exist!" << endl;
        exit (EXIT_FAILURE);
    }
    else
    {
        filefound = 1;
    }
} while (filefound == 0);

cout << endl << "Loading..." << endl;

return;
}

Solution

  • what is exit (EXIT_FAILURE); for?