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. how to open a txt file (using QFile) getting current file from Qdir?
QtWS25 Last Chance

how to open a txt file (using QFile) getting current file from Qdir?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 9.1k 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.
  • P Offline
    P Offline
    pedromenezes
    wrote on last edited by
    #1

    Hi,
    i'm using qt creator (macos) and i need to open a txt file file using QFile from the current directory using Q Dir.

    code:

    QFile file(QDir::current().absolutePath() + "/peatefinal.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

    This code already get the path but it don't open the file. I tried "currentPath ()" as well but it didn't work too. Think that the problem could be related to the separator because the current path uses "/" and a need to use "//" to open manually. How can i solve this problem?

    Cheers

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi and welcome to the forums.
      did you try
      qDebug() << QDir::current().absolutePath();
      to make sure its the path you expect ?
      Also can you explain a bit more what you mean by having to use
      "//" to open manually ?

      1 Reply Last reply
      2
      • P Offline
        P Offline
        pedromenezes
        wrote on last edited by
        #3

        @mrjj said in how to open a txt file (using QFile) getting current file from Qdir?:

        qDebug() << QDir::current().absolutePath();

        Thank you very much for your answer. Now i guess that the problem is how can i put the txt file inside the directory. The file "peatefinal.txt" is saved on the same directory of the project but it didn't recognize the file when i run the project:

        Code:
        QFile file( "peatefinal.txt");
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

        When i save the file to into the main directory and changed the code like this:

        Code:
        QFile file( "//peatefinal.txt");
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

        It works perfectly.

        So the problem now is: How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?

        Pl45m4P 1 Reply Last reply
        0
        • P pedromenezes

          @mrjj said in how to open a txt file (using QFile) getting current file from Qdir?:

          qDebug() << QDir::current().absolutePath();

          Thank you very much for your answer. Now i guess that the problem is how can i put the txt file inside the directory. The file "peatefinal.txt" is saved on the same directory of the project but it didn't recognize the file when i run the project:

          Code:
          QFile file( "peatefinal.txt");
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;

          When i save the file to into the main directory and changed the code like this:

          Code:
          QFile file( "//peatefinal.txt");
          if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
          return;

          It works perfectly.

          So the problem now is: How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:

          How can i save the txt file inside the same directory of the app if the file is already saved on the same directory of the project?

          So you want to save your txt file in the directory where your program is executed?

          // This returns your appDir
          QCoreApplication::applicationDirPath()
          

          Idk if this is what you are looking for.

          The file is saved in your app (which app? Qt Creator or your program?) directory and you want to (move?) save it in your project directory?


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          3
          • P Offline
            P Offline
            pedromenezes
            wrote on last edited by pedromenezes
            #5

            @Pl45m4 said in how to open a txt file (using QFile) getting current file from Qdir?:

            QCoreApplication::applicationDirPath()

            Thank's for your help. The app I'm doing "DirOpen2" reads some .txt files and plots on a chart. It turns out, I do not know why, even these files being in the same folder as my app I can not read them. It looks like they are not being copied to the same folder as my application.

            The code below did not find the txt file in the app appendix (this folder seems to be hidden i did not find it by the Finder):

            QFile file(QCoreApplication::applicationDirPath() + "/peatefinal.txt");
                      ui->label->setText(QCoreApplication::applicationDirPath() + "/peatefinal.txt");
                       if(!file.exists()){
                           ui->textBrowser->setText("The file was not found");
                       }
                       if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
                          return;
              ui->label_2->setText("The file was not opened");
            

            The first label is: /Users/pedromenezes/Desktop/00_Arquivos/04_Profissional/05_LATEC/03_Desenvolvimento/x_Testes/build-OpenDir2-Desktop_Qt_5_12_0_clang_64bit-Debug/OpenDir2.app/Contents/MacOS

            The textBox shows: "The file was not found"

            But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.

            However, if I save the file of interest ("peatefinal.txt") in the main directory and change the code to:

            QFile file("//peatefinal.txt");
            

            it works.

            This file: "peatefinal.txt" is already saved in the same folder of the project. But if i change the code to:

            QFile file("peatefinal.txt");
            

            it doesn't work.

            cheers

            M 1 Reply Last reply
            0
            • P pedromenezes

              @Pl45m4 said in how to open a txt file (using QFile) getting current file from Qdir?:

              QCoreApplication::applicationDirPath()

              Thank's for your help. The app I'm doing "DirOpen2" reads some .txt files and plots on a chart. It turns out, I do not know why, even these files being in the same folder as my app I can not read them. It looks like they are not being copied to the same folder as my application.

              The code below did not find the txt file in the app appendix (this folder seems to be hidden i did not find it by the Finder):

              QFile file(QCoreApplication::applicationDirPath() + "/peatefinal.txt");
                        ui->label->setText(QCoreApplication::applicationDirPath() + "/peatefinal.txt");
                         if(!file.exists()){
                             ui->textBrowser->setText("The file was not found");
                         }
                         if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
                            return;
                ui->label_2->setText("The file was not opened");
              

              The first label is: /Users/pedromenezes/Desktop/00_Arquivos/04_Profissional/05_LATEC/03_Desenvolvimento/x_Testes/build-OpenDir2-Desktop_Qt_5_12_0_clang_64bit-Debug/OpenDir2.app/Contents/MacOS

              The textBox shows: "The file was not found"

              But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.

              However, if I save the file of interest ("peatefinal.txt") in the main directory and change the code to:

              QFile file("//peatefinal.txt");
              

              it works.

              This file: "peatefinal.txt" is already saved in the same folder of the project. But if i change the code to:

              QFile file("peatefinal.txt");
              

              it doesn't work.

              cheers

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:

              But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.

              It's the name of your app in the buid folder.
              To look at "OpenDir2.app/Contents/MacOS"
              right click on your app and choose "show package contents" and navigate to MacOs folder.

              Anyway, using the current path on a desktop app is meaningless AMHA. On Mac the current path is not the same if you run your app from the desktop !

              Use QStandardPaths class to read/save your files to standard locations.

              P 1 Reply Last reply
              5
              • M mpergand

                @pedromenezes said in how to open a txt file (using QFile) getting current file from Qdir?:

                But i didn't find this folder "OpenDir2.app" maybe it is hidden. I don't know.

                It's the name of your app in the buid folder.
                To look at "OpenDir2.app/Contents/MacOS"
                right click on your app and choose "show package contents" and navigate to MacOs folder.

                Anyway, using the current path on a desktop app is meaningless AMHA. On Mac the current path is not the same if you run your app from the desktop !

                Use QStandardPaths class to read/save your files to standard locations.

                P Offline
                P Offline
                pedromenezes
                wrote on last edited by
                #7

                @mpergand Thank you very much. Problem solved!

                J.HilkJ 1 Reply Last reply
                0
                • P pedromenezes

                  @mpergand Thank you very much. Problem solved!

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @pedromenezes
                  maybe you want to look into QStandardPath -class, could come in handy for you as well

                  Don't forget to set the topic to solved when your question is answered ;-)


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    On a side note: don't modify the application bundle content like that. It's bad practice and it will also likely fail depending on how your application will be deployed.

                    As @J-Hilk suggested, QStandardPaths to get the correct folder where to store your application data.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • P Offline
                      P Offline
                      pedromenezes
                      wrote on last edited by pedromenezes
                      #10

                      Thanks again! I solved the problem using:

                      QString path = qApp->applicationDirPath(); 
                      path.append("/peate.txt"); 
                      QFile fileUrl(path);
                      

                      But I will study the QStandardPaths to use it.

                      1 Reply Last reply
                      2

                      • Login

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