Search code examples
c++file-ioqt-creatorfstreamifstream

Failing File I/O is_open() in Qt Creator


I'm using QtCreator 3.6.0 - Based on Qt 5.5.1 (Clang 6.1 (Apple), 64 bit).

I'm trying to read a text file but I keep on getting this error message,

Unable to open file/Users/... ...

Here is the code extract,

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

My 'Config.txt' file is in the same project folder where main.cpp is.

Where am I going wrong?


Solution

  • For QtCreator, you should change the working directory to the directory containing Config.txt. You can find the "Working directory" setting for your executable via the sidebar "Projects" => "Build & Run" => "Run"

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