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

标签 c++ file loops ifstream

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

但是,循环只经过一次。它打印出“文件错误:文件不存在!”然后结束程序。有任何想法吗?如果您需要更多信息,请告诉我!

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

关于C++ 创建一个函数来读取 .txt 文件并检查以确保文件存在,但我的循环不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53578358/

相关文章:

c++ - 解析 64 位十六进制数

r - 用于在 R 中生成组合的动态 for 循环

linux - 如何使用 shell 在 linux 中逐一打印具有完整信息的所有用户列表

c - 对于我的 C 代码,我读取文件并对它进行排序会产生垃圾输出。为什么?

c++ - 用C++在文件中输入长短语

c# - 如何在c#.net中的多线程和每行一个线程中逐行处理文件?

javascript - jQuery 迭代嵌套 html 列表

c++ - C++中的空白字符串

c++ - 如何处理文件中的位?

c++ - Template Specialization pointer to pointer and array passing template deduction