C++ 创建一个函数来读取 .txt 文件并检查以确保文件存在,但我的循环不工作

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

我正在尝试创建一个函数来读取文件,检查以确保它存在(如果它不再询问文件名),然后打印 "Loading..." 如果它存在找到文件。

但是,循环只经过一次。它打印出 "FILE ERROR: File does not exist!" 然后结束程序。有任何想法吗?如果您需要更多信息,请告诉我!

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

exit (EXIT_FAILURE); 有什么用?

我在这里看到的问题是:

退出 (EXIT_FAILURE);

如果您想在 none 现有文件名后循环 - 您需要删除此行!