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 and writing same file

Reading and writing same file

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 6 Posters 4.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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by
    #1

    Hi,
    I log some data from my application to QFile (via QTextStream).

    Sometimes I want to read all data in the file and display them. How to do it (at the same time, the file must be still opened for writing)?

    jsulmJ Ni.SumiN 2 Replies Last reply
    0
    • ndt_mikeN Offline
      ndt_mikeN Offline
      ndt_mike
      wrote on last edited by
      #2

      QIODevice::ReadWrite should do the trick...check out the documentation on QFile and QIODevice.

      Not really sure how you are going to read the file while writing to it at the same time, kinda be a mess, but you can open it for ReadWrite access

      1 Reply Last reply
      0
      • M MartinD

        Hi,
        I log some data from my application to QFile (via QTextStream).

        Sometimes I want to read all data in the file and display them. How to do it (at the same time, the file must be still opened for writing)?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @MartinD I would not read/write at the same time as it will most probably cause problems. If you want to read close the file, open it for reading, read the content, close it, open for writing again.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M MartinD

          Hi,
          I log some data from my application to QFile (via QTextStream).

          Sometimes I want to read all data in the file and display them. How to do it (at the same time, the file must be still opened for writing)?

          Ni.SumiN Offline
          Ni.SumiN Offline
          Ni.Sumi
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • M Offline
            M Offline
            MartinD
            wrote on last edited by
            #5

            Thanks, I will close the file momentarily, read it, and then reopen it for append.

            1 Reply Last reply
            0
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

              Hi,

              As said by @jsulm and @ndt_mike , while doing file operations
              Once u open the file either for reading the data or writing the data.
              Use file.close(); .
              Once u finished reading the data or writing the data, and for the next operations u can open the file and do the further necessary operations,then again use file.close();

              Link to how to use QFile for reading and writing data.

              http://doc.qt.io/qt-5/qfile.html

              And here is the link the enum values which will be used.

              http://doc.qt.io/qt-4.8/qiodevice.html#OpenModeFlag-enum

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              2
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Since a file is seekable I'd exploit this feature. Open the file in QIODevice::ReadWrite then

                const qint64 pos = file.pos(); //Save the current position
                file.seek(0);  // move back to beginning of the file
                // read what you need
                file.seek(pos); // go back to where you were before reading
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                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