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. Using text file in resources for read and write in to text file.
Forum Update on Monday, May 27th 2025

Using text file in resources for read and write in to text file.

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 2.0k 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.
  • T Offline
    T Offline
    TheCeylann
    wrote on 16 Feb 2022, 10:30 last edited by
    #1

    I wrote a code to take the text in QLineEdit and write it to the text file. But when giving the file path of the text file, I use the file path of my computer, for example C:\Users\pc\Desktop\blabla/blabla.txt. But when I run this program on another computer, it will not find the file path. I tried adding a text file to the Resources section, but I could not write in the file. How can I do that. I'm very new to QT and c++ and an amateur.

    void SecondWindow::on_pushButton_8_clicked()
    {
    
        QFile file(":/new/text/text/test.txt");
    
        if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
            return;
        {
            QTextStream  out(&file);
            out << ui->newPassBox->text();
    
        }
    
        }
    

    Here ıs my resources folder

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 16 Feb 2022, 10:33 last edited by
      #2

      Resource files are read-only (by design and not even possible to write during runtime when you think about it)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply 16 Feb 2022, 10:44
      2
      • C Christian Ehrlicher
        16 Feb 2022, 10:33

        Resource files are read-only (by design and not even possible to write during runtime when you think about it)

        T Offline
        T Offline
        TheCeylann
        wrote on 16 Feb 2022, 10:44 last edited by TheCeylann
        #3

        @Christian-Ehrlicher How can I write in to text while program is running. Do you now any way to do this?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 16 Feb 2022, 10:46 last edited by
          #4

          Write it to a real file. See also QStandardPaths.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply 16 Feb 2022, 11:27
          2
          • C Christian Ehrlicher
            16 Feb 2022, 10:46

            Write it to a real file. See also QStandardPaths.

            T Offline
            T Offline
            TheCeylann
            wrote on 16 Feb 2022, 11:27 last edited by
            #5

            @Christian-Ehrlicher I can print to file. What I need is for the application to find the file path when I run it on another computer. How can I do that?

            C 1 Reply Last reply 16 Feb 2022, 11:36
            0
            • T TheCeylann
              16 Feb 2022, 11:27

              @Christian-Ehrlicher I can print to file. What I need is for the application to find the file path when I run it on another computer. How can I do that?

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 16 Feb 2022, 11:36 last edited by
              #6

              @TheCeylann said in Using text file in resources for read and write in to text file.:

              How can I do that?

              Someone has to copy the file from computer A to computer B. Or store the file somewhere in the internet where you can access it from every computer with internet connection. Don't know your usecase though.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              T 1 Reply Last reply 17 Feb 2022, 07:53
              0
              • C Christian Ehrlicher
                16 Feb 2022, 11:36

                @TheCeylann said in Using text file in resources for read and write in to text file.:

                How can I do that?

                Someone has to copy the file from computer A to computer B. Or store the file somewhere in the internet where you can access it from every computer with internet connection. Don't know your usecase though.

                T Offline
                T Offline
                TheCeylann
                wrote on 17 Feb 2022, 07:53 last edited by
                #7

                @Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?

                B J 2 Replies Last reply 17 Feb 2022, 08:39
                0
                • T TheCeylann
                  17 Feb 2022, 07:53

                  @Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?

                  B Offline
                  B Offline
                  Bonnie
                  wrote on 17 Feb 2022, 08:39 last edited by
                  #8

                  @TheCeylann You can use QCoreApplication::applicationDirPath(), like

                  QCoreApplication::applicationDirPath() + "/test.txt"
                  
                  C 1 Reply Last reply 17 Feb 2022, 08:40
                  0
                  • B Bonnie
                    17 Feb 2022, 08:39

                    @TheCeylann You can use QCoreApplication::applicationDirPath(), like

                    QCoreApplication::applicationDirPath() + "/test.txt"
                    
                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 17 Feb 2022, 08:40 last edited by
                    #9

                    @Bonnie This is the completely wrong directory - see my answer above: https://forum.qt.io/topic/134385/using-text-file-in-resources-for-read-and-write-in-to-text-file/6

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    B 1 Reply Last reply 17 Feb 2022, 08:53
                    0
                    • T TheCeylann
                      17 Feb 2022, 07:53

                      @Christian-Ehrlicher I will store the password inside the text file. Uploading it somewhere can be a problem. Is there a way to define the file inside the file folder instead of defining it as full path. We can call files that are in the same folder like HTML only by their names. Is there a way to do this in Qt c++?

                      J Offline
                      J Offline
                      JonB
                      wrote on 17 Feb 2022, 08:42 last edited by
                      #10

                      @TheCeylann
                      I don't know what your last post means. But yes you can specify a plain filename without a path to QFile(). The problem is where do you think that is sought relative to? It will be the current working directory your application has when it's running. And you essentially have no control over that, and don't know what it might be. It will not necessarily have any relation at all to where your executable is installed or where your other files involved in the build/installation might be.

                      That is why you should use something based off QStandardPaths, so you know where it actually is.

                      1 Reply Last reply
                      0
                      • C Christian Ehrlicher
                        17 Feb 2022, 08:40

                        @Bonnie This is the completely wrong directory - see my answer above: https://forum.qt.io/topic/134385/using-text-file-in-resources-for-read-and-write-in-to-text-file/6

                        B Offline
                        B Offline
                        Bonnie
                        wrote on 17 Feb 2022, 08:53 last edited by
                        #11

                        @Christian-Ehrlicher
                        The OP ask for a HTML-like way, so I feel he just want to store the file with the exe.
                        I'm sure he should also learn to use QStandardPaths. :)

                        J 1 Reply Last reply 17 Feb 2022, 08:55
                        1
                        • B Bonnie
                          17 Feb 2022, 08:53

                          @Christian-Ehrlicher
                          The OP ask for a HTML-like way, so I feel he just want to store the file with the exe.
                          I'm sure he should also learn to use QStandardPaths. :)

                          J Offline
                          J Offline
                          JonB
                          wrote on 17 Feb 2022, 08:55 last edited by
                          #12

                          @Bonnie
                          But OP is asking to be able to write to file too, right? "HTML-like way" is not for writing to files, is it?

                          B 1 Reply Last reply 17 Feb 2022, 09:06
                          0
                          • J JonB
                            17 Feb 2022, 08:55

                            @Bonnie
                            But OP is asking to be able to write to file too, right? "HTML-like way" is not for writing to files, is it?

                            B Offline
                            B Offline
                            Bonnie
                            wrote on 17 Feb 2022, 09:06 last edited by
                            #13

                            @JonB Right, but there're still quite many portable applications choose to store their configuration file, which could be both read and written, with the exe file.
                            QStandardPaths is not very friendly to portable applications though.

                            J 1 Reply Last reply 17 Feb 2022, 09:07
                            0
                            • B Bonnie
                              17 Feb 2022, 09:06

                              @JonB Right, but there're still quite many portable applications choose to store their configuration file, which could be both read and written, with the exe file.
                              QStandardPaths is not very friendly to portable applications though.

                              J Offline
                              J Offline
                              JonB
                              wrote on 17 Feb 2022, 09:07 last edited by JonB
                              #14

                              @Bonnie said in Using text file in resources for read and write in to text file.:

                              which could be both read and written, with the exe file.

                              Really? Under Linux??

                              And nowadays under Windows apps are installed into Program Files and Win 10+ doesn't allow users to write there any more, does it?

                              B 1 Reply Last reply 17 Feb 2022, 09:08
                              0
                              • J JonB
                                17 Feb 2022, 09:07

                                @Bonnie said in Using text file in resources for read and write in to text file.:

                                which could be both read and written, with the exe file.

                                Really? Under Linux??

                                And nowadays under Windows apps are installed into Program Files and Win 10+ doesn't allow users to write there any more, does it?

                                B Offline
                                B Offline
                                Bonnie
                                wrote on 17 Feb 2022, 09:08 last edited by Bonnie
                                #15

                                @JonB No, of course Windows!
                                Portable applications are those without installers, they wouldn't be installed into Program Files :)

                                J 1 Reply Last reply 17 Feb 2022, 09:09
                                0
                                • B Bonnie
                                  17 Feb 2022, 09:08

                                  @JonB No, of course Windows!
                                  Portable applications are those without installers, they wouldn't be installed into Program Files :)

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 17 Feb 2022, 09:09 last edited by JonB
                                  #16

                                  @Bonnie
                                  I didn't know it was Windows-only, thought it might be X-platform. But I had added to my post that nowadays you cannot write into Windows Program Files, can you?
                                  Oh OK, you & I are cross-posting now.
                                  Sometimes I run "portable apps" off a read-only USB stick....

                                  QStandardPaths is not very friendly to portable applications though.

                                  What about e.g. QStandardPaths::AppDataLocation or other App... ones?

                                  B 1 Reply Last reply 17 Feb 2022, 09:20
                                  0
                                  • J JonB
                                    17 Feb 2022, 09:09

                                    @Bonnie
                                    I didn't know it was Windows-only, thought it might be X-platform. But I had added to my post that nowadays you cannot write into Windows Program Files, can you?
                                    Oh OK, you & I are cross-posting now.
                                    Sometimes I run "portable apps" off a read-only USB stick....

                                    QStandardPaths is not very friendly to portable applications though.

                                    What about e.g. QStandardPaths::AppDataLocation or other App... ones?

                                    B Offline
                                    B Offline
                                    Bonnie
                                    wrote on 17 Feb 2022, 09:20 last edited by Bonnie
                                    #17

                                    @JonB No, a typical usage of a portable application is coping its folder to an USB disk and you can put it on different computers and run it with remembering the configurations you already made.
                                    How could the App... ones do that?
                                    Sure there will be unwritable risks, but if the portablity is more important, then I have to accept that.

                                    J 1 Reply Last reply 17 Feb 2022, 09:30
                                    0
                                    • B Bonnie
                                      17 Feb 2022, 09:20

                                      @JonB No, a typical usage of a portable application is coping its folder to an USB disk and you can put it on different computers and run it with remembering the configurations you already made.
                                      How could the App... ones do that?
                                      Sure there will be unwritable risks, but if the portablity is more important, then I have to accept that.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 17 Feb 2022, 09:30 last edited by JonB
                                      #18

                                      @Bonnie said in Using text file in resources for read and write in to text file.:

                                      How could the App... ones do that?

                                      Perhaps you had understood that the OP wishes to write to a file and then take that updated file to another computer, but I had not. Clearly that changes the equation! :)

                                      1 Reply Last reply
                                      0

                                      5/18

                                      16 Feb 2022, 11:27

                                      13 unread
                                      • Login

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