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. Reading a file avoids writing to it
Qt 6.11 is out! See what's new in the release blog

Reading a file avoids writing to it

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 2.6k Views 1 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.
  • A Offline
    A Offline
    Asohen
    wrote on last edited by
    #1

    Hi all!
    I'm facing a file handling problem that I don't understand (yet :-)

    There is one application writing a short line into a text file, every minute. Its source code is not available.
    My application opens this QFile QIODevice::ReadOnly | QIODevice::Text and reads from it every 10 seconds. The file leaves opened all time.

    Now following happens: The first application stops writing, until my app. is aborted manually.
    That means: Reading the file avoids writing to it.
    Why? Are there any hints?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Welcome to devnet

      The file is opened exclusively for writing and simply is not opened when opened by another process.

      Alternatively, if there is always only this one line in the file, I would assume that the file is freshly opened each time for writing. It should be similar to open a file without closing it. In your case the content will be written to the new file, which you have NOT opened. Therefore, you cannot find the new information.

      The only chances I see is that you are opening repeatably as well. However, you have to ensure that the file is not opened while the other application is writing.

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asohen
        wrote on last edited by
        #3

        Hi koahnig,
        thanks for your post.

        I already tried to open with ReadWrite flags, to allow the 1st application to go on writing. But it doesn't change anything.

        Indeed I'm thinking about "open on demand":
        -Wait till file date changed

        • open file
        • read
        • close
          That should work, but pretty looks like a workaround...
        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [quote author="Asohen" date="1422182419"]
          I already tried to open with ReadWrite flags, to allow the 1st application to go on writing. But it doesn't change anything.
          [/quote]
          ReadWrite flag is very likely worse than ReadOnly. First of all it describes the handling where you are opening. Since your new applciation does not require writing permission, ReadOnly shall be sufficient.
          Furthermore, a ReadWrite flag shall reserve the file for your application and the other application has no chance to open it.

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asohen
            wrote on last edited by
            #5

            [quote author="koahnig" date="1422188171"]ReadWrite flag shall reserve the file for your application and the other application has no chance to open it.
            [/quote]
            That behaviour is plausible.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Why do you read the file every ten seconds, instead of only after it has changed?

              If you use a QFileSystemWatcher to monitor the file, you can do your reading when there is actually something new to read. That will also lower the risk of you blocking the write with your reading.

              Other than that: do your reading as fast as you can, and close the file again. Only after your reading, you start processing its contents. Fastest is probably to use QFile::readAll to read the contents of your file to a QByteArray. Then you can use a QBuffer on your QByteArray to access it as if it is the file itself.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asohen
                wrote on last edited by
                #7

                Indeed, I have to change reading. The file will be closed until the writing application appends data to it. Then I will open it again, read and close, and so on.
                No problem so far.

                But I started this thread to understand, why the writing thread stops writing as soon as my reading application reads. This point is not clear yet.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  cdietz
                  wrote on last edited by
                  #8

                  If you are on windows, your application blocks the file's handle.
                  The other application cannot open/write to it until your application releases the file's handle again (by closing it).

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DBoosalis
                    wrote on last edited by
                    #9

                    Are you using the file for some kind of communication, if so perhaps you ought to use sockets between the two applications, the writer app being the server and the reader app being the client. You can still write to the file, if data needs to be saved

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Asohen
                      wrote on last edited by
                      #10

                      [quote author="cdietz" date="1422367650"]If you are on windows, your application blocks the file's handle.[/quote]
                      Thanks for the hint. "Of course" I'm on Windows :-|

                      OK, it's a characteristic of windows - not my clumsy programming.
                      Changing to a open-read-close logic.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Asohen
                        wrote on last edited by
                        #11

                        [quote author="DBoosalis" date="1422371587"]Are you using the file for some kind of communication, if so perhaps you ought to use sockets between the two applications[/quote]
                        That's the usual method, but unfortunately the writers sources are not open.

                        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