Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved QFile can't find the file

    General and Desktop
    3
    5
    2652
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aesthetically asthmatic last edited by aesthetically asthmatic

      I'm trying to read data from a .txt file but my QFile can't read the data from it.
      I've tried using "F:/Database/visi.txt" as the address and it works fine. But when I try to use "visi.txt" and put the file in the Debug folder and also the project folder, it can't read it anywhere. I've also tried using QCoreApplication::applicationDirPath() + "visi.txt" but it still can't seem to read it.
      I'm new to Qt and don't know how to fix it. Maybe it is because that my Project, Debug and Release folder is on the Desktop. I want my .txt to be in the same folder as the project/application and still be able to read it if I change the directory folder of the application with the txt file.

      K 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi and welcome the forums.
        I assume you need it outside the app (editable), so using qresource is a no go.

        QCoreApplication::applicationDirPath() is the right way but i think you need

        QString fullpath = QCoreApplication::applicationDirPath() + "/visi.txt"
        if inside the database folder that is NEXT to the exe. it should be
        QString fullpath = QCoreApplication::applicationDirPath() + "/DataBase/visi.txt"

        1 Reply Last reply Reply Quote 5
        • K
          koahnig @aesthetically asthmatic last edited by

          @aesthetically-asthmatic

          Hi and welcome to devnet forum

          This one QCoreApplication::applicationDirPath() + "visi.txt" should be probably QCoreApplication::applicationDirPath() + "/visi.txt" otherwise you are changing only the last folder name and extend it.

          If you go on the left pane to "projects" and check under "Build&Run" -> "Run" you can see the working directory when you start your application through Qt creator

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply Reply Quote 3
          • A
            aesthetically asthmatic last edited by

            @koahnig @mrjj
            I tried to do it using
            QCoreApplication::applicationDirPath() + "/visi.txt"
            but my program was still unable to open it.

            QString fEmp = QCoreApplication::applicationDirPath() + "/visi.txt";
            QFile ReadFrom(fEmp);
                if(!ReadFrom.open(QFile::ReadOnly | QFile::Text)) {
                    QMessageBox::information(this, "File Error", "This file does not exist or there was an error while reading it.");
                    return;
                }
            

            This is the code I'm using to open the file. Everytime I press the button where this code is connected to, it gives me the message written in QMessageBox. I put the visi.txt in all the folders: Debug, Release and the project folder but the error was still there.

            mrjj 1 Reply Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @aesthetically asthmatic last edited by

              @aesthetically-asthmatic
              put it next to where .exe is.
              also do
              qDebug() << fEmp;
              and check the path.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post