Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile can't find the file
Forum Update on Monday, May 27th 2025

QFile can't find the file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.8k Views
  • 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 Offline
    A Offline
    aesthetically asthmatic
    wrote on last edited by aesthetically asthmatic
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      5
      • A 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 Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @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
        3
        • A Offline
          A Offline
          aesthetically asthmatic
          wrote on last edited by
          #4

          @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.

          mrjjM 1 Reply Last reply
          0
          • A aesthetically asthmatic

            @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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved