【问题标题】:Failing File I/O is_open() in Qt CreatorQt Creator 中的文件 I/O is_open() 失败
【发布时间】:2016-03-29 11:15:10
【问题描述】:

我正在使用 QtCreator 3.6.0 - 基于 Qt 5.5.1(Clang 6.1 (Apple),64 位)。

我正在尝试读取文本文件,但我不断收到此错误消息,

无法打开文件/用户/... ...

这里是代码摘录,

string line;
    ifstream myfile ("Config.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    }

    else cout << "Unable to open file";

我的 'Config.txt' 文件位于 main.cpp 所在的同一个项目文件夹中。

我哪里错了?

【问题讨论】:

  • 打印程序运行的cwd。
  • 你确定是输出对应的代码吗?
  • 您是否尝试将文件与生成的可执行文件放在同一文件夹中?
  • QtCreator 不会执行您的main.cpp 所在文件夹中的程序。
  • 指定文件的完整路径

标签: c++ file-io qt-creator fstream ifstream


【解决方案1】:

对于 QtCreator,您应该将工作目录更改为包含 Config.txt 的目录。您可以通过侧边栏“Projects”=>“Build & Run”=>“Run”找到可执行文件的“Working directory”设置

见:https://doc.qt.io/qtcreator/creator-run-settings.html

【讨论】:

    猜你喜欢
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2010-11-06
    相关资源
    最近更新 更多