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 rename a file while writing to file continuously?
Forum Updated to NodeBB v4.3 + New Features

How to rename a file while writing to file continuously?

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 6 Posters 1.4k Views 2 Watching
  • 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.
  • S Offline
    S Offline
    Shankar B
    wrote on last edited by
    #1

    However, QFile::rename() sometimes work, sometimes do not work, I even tried to close the QFile manually before rename and remove, but this do not fixed this issue either.

    I need to rename current file (log_filename_0 to log_filename_1 ) when file size exceeds and again i need to write into same file log_filename_0.

    J.HilkJ 1 Reply Last reply
    0
    • S Shankar B

      However, QFile::rename() sometimes work, sometimes do not work, I even tried to close the QFile manually before rename and remove, but this do not fixed this issue either.

      I need to rename current file (log_filename_0 to log_filename_1 ) when file size exceeds and again i need to write into same file log_filename_0.

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

      @Shankar-B

      QFile f1(log_filename_0);
      if(f1.open(QIODevice:: WriteOnly) {
      .....
      }
      f1.close();
      
      QFile::copy(log_filename_0, logfilename_1); // returns true if successful
      
      if(f1.open(QIODevice:: WriteOnly) {
      .....
      }
      
      
      
      

      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
      5
      • S Offline
        S Offline
        Shankar B
        wrote on last edited by
        #3

        My requirement is when I have opened a file in an instance of my app. When I try to open the same file in another instance of the app I should know that the file is already opened. Since it is another instance which means another QFile object.
        I need to rename file in second instance only if file is closed in first instance.

        Christian EhrlicherC 1 Reply Last reply
        0
        • S Shankar B

          My requirement is when I have opened a file in an instance of my app. When I try to open the same file in another instance of the app I should know that the file is already opened. Since it is another instance which means another QFile object.
          I need to rename file in second instance only if file is closed in first instance.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Shankar-B This is not possible (neither with Qt nor with another low-level api afaik) - you have to think about another way to achieve this. Why do you want to open a file from two different instances?

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

          S 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Are you thinking of something like logrotate ?

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

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Are you thinking of something like logrotate ?

              S Offline
              S Offline
              Shankar B
              wrote on last edited by
              #6

              @SGaist yes

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @Shankar-B This is not possible (neither with Qt nor with another low-level api afaik) - you have to think about another way to achieve this. Why do you want to open a file from two different instances?

                S Offline
                S Offline
                Shankar B
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                There are two application, first application will open file and write logs in to file.
                second application will monitor the file size which is opened by first application.
                if file size exceeds i need to rename the file from second application.

                How can i know in second application file is closed or not?

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Shankar-B said in How to rename a file while writing to file continuously?:

                  How can i know in second application file is closed or not?

                  You can't but why needs the second app do the rename? Can't this be done by the first app? And what are you try to achieve?

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

                  S 1 Reply Last reply
                  0
                  • S Shankar B

                    @Christian-Ehrlicher
                    There are two application, first application will open file and write logs in to file.
                    second application will monitor the file size which is opened by first application.
                    if file size exceeds i need to rename the file from second application.

                    How can i know in second application file is closed or not?

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Shankar-B
                    You cannot do what you want. Firstly, you cannot rename in second app what is currently open (via a file handle) in first app (likely OS limitation), and secondly even if you could how would first application know and start writing to a new log file?

                    Even if file is closed in first app, it will lead to a (potential) race condition if you have second app renaming file at an instant where first file might be trying to add a new log to it.

                    S 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Shankar-B said in How to rename a file while writing to file continuously?:

                      How can i know in second application file is closed or not?

                      You can't but why needs the second app do the rename? Can't this be done by the first app? And what are you try to achieve?

                      S Offline
                      S Offline
                      Shankar B
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher
                      My second application is logmanagement application. So renaming log files am doing it in my second application.
                      my first application will write logs in file, while writing if file size exceeds i need to rename from second application.Size monitoring am doing in my second application.

                      1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Shankar-B
                        You cannot do what you want. Firstly, you cannot rename in second app what is currently open (via a file handle) in first app (likely OS limitation), and secondly even if you could how would first application know and start writing to a new log file?

                        Even if file is closed in first app, it will lead to a (potential) race condition if you have second app renaming file at an instant where first file might be trying to add a new log to it.

                        S Offline
                        S Offline
                        Shankar B
                        wrote on last edited by
                        #11

                        @JonB
                        Can we do this by file share.

                        JonBJ 1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          See my first post - it's not that easily possible. Do it from your first application as every other logger does.

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

                          1 Reply Last reply
                          0
                          • S Shankar B

                            @JonB
                            Can we do this by file share.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #13

                            @Shankar-B said in How to rename a file while writing to file continuously?:

                            Can we do this by file share.

                            Whether by "file share" (doesn't sound very Qt, platform-independent) or by locking, I think you will have implementation and/or race problems. The logging app would have to take & release exclusive access across each log action. The renaming app would have to take exclusive and then, while holding it, do rename, then release. This may not be allowed/work on OS, or it may be implemented non-atomically/robustly. Difficult to test reliably. Platform-specific, you'd have to read up. If tried from Qt, may well rely on what it uses for platform implementation, which is not documented/ to be relied on.

                            It really isn't a good idea to try to do it this way.

                            Also, if you mean literally

                            while writing if file size exceeds i need to rename from second application

                            you cannot go renaming a file right in the middle of writing to it.

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

                              @Shankar-B what system are you targeting with that application ?

                              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
                              0
                              • S Offline
                                S Offline
                                SimonSchroeder
                                wrote on last edited by
                                #15

                                One major point is that the writing app needs to close the file and reopen it. On every OS you have a handle to a file, i.e. more or less a position on the disk. Directly after opening the name of the file is not important anymore for your file handle. Linux would (most likely) let you rename the file while it is open (at least removing works on open files). What would happen in this case is that entries in the file system would change, however the app would still point to the same place on the disk and continue writing on the file it has opened (the name is only to find the file, but it is not the file itself).

                                So, in order to work correctly the writing app needs to have the logic: It needs to know when to change to a different file. Then it could be this app itself to rename the file. Close, rename, reopen. If you are writing to the same log file from multiple apps this approach does not work at all. The only way would be to open, then write, and immediatly close the file again. This, however, will be really slow.

                                The best suggestion I have is that you extend your logmanager. Apps should be able to connect to the logmanager through a pipe. Each app then writes to its own pipe and the logmanager reads from the pipes and writes to the file. Then the logmanager can really manage the file.

                                JonBJ 1 Reply Last reply
                                3
                                • S SimonSchroeder

                                  One major point is that the writing app needs to close the file and reopen it. On every OS you have a handle to a file, i.e. more or less a position on the disk. Directly after opening the name of the file is not important anymore for your file handle. Linux would (most likely) let you rename the file while it is open (at least removing works on open files). What would happen in this case is that entries in the file system would change, however the app would still point to the same place on the disk and continue writing on the file it has opened (the name is only to find the file, but it is not the file itself).

                                  So, in order to work correctly the writing app needs to have the logic: It needs to know when to change to a different file. Then it could be this app itself to rename the file. Close, rename, reopen. If you are writing to the same log file from multiple apps this approach does not work at all. The only way would be to open, then write, and immediatly close the file again. This, however, will be really slow.

                                  The best suggestion I have is that you extend your logmanager. Apps should be able to connect to the logmanager through a pipe. Each app then writes to its own pipe and the logmanager reads from the pipes and writes to the file. Then the logmanager can really manage the file.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @SimonSchroeder said in How to rename a file while writing to file continuously?:

                                  Linux would (most likely) let you rename the file while it is open (at least removing works on open files).

                                  True for Linux. But for Windows I think not. (It would be nice if OP stated which platform(s) are being targetted for a question like this.) E.g.
                                  https://docs.microsoft.com/en-us/windows/win32/fileio/moving-and-replacing-files

                                  Before a file can be copied, it must be closed or opened only for reading. No thread can have the file opened for writing.
                                  A file must also be closed before an application can move it.

                                  As for:

                                  The only way would be to open, then write, and immediatly close the file again.

                                  This may be required anyway, even if only one instance of OP's logging app. In order for the renaming governor app to "see" that the log file has grown it will need to use QFileSystemWatcher::fileChanged(). It may (well) be the case that this does not fire when data is being written on an open file handle until that file handle is closed. Certainly under Windows this would not surprise me, e.g. you tend to see file sizes being shown as 0 while being written to until writing is finished and they are closed. Not sure under Linux. Would have to test.

                                  1 Reply Last reply
                                  0

                                  • Login

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