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. QFile write to front

QFile write to front

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 4.8k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I'm writing a library for encoding audio data and writing it to file. I encode 1 chunk of data at a time and write it to file. Only once the encoding is done, I can calculate the header data. This data has to be added to the front of a QFile. Is there any way I can do this?
    I thought of writing some empty data at the start and replace it with the header at the end. This is not possible; since I don't know the size of the header (I can only calculate the header size once all data has been written).

    1 Reply Last reply
    0
    • E Offline
      E Offline
      esdrasbeleza
      wrote on last edited by
      #2

      I don't know if my suggestion works, but did you try to use a QBuffer? You can read your file using a QBuffer, move to its start using seek(0) and try to write your data in the beginning.

      Edit: I just read here, QFile also has the seek() and writeData() functions. Did you try to use them?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goocreations
        wrote on last edited by
        #3

        Jip, that's the second thing I mentioned. When you seek(0) and then write, it overwrites the first n bytes of your data (where n is the size of your header). So it doesn't insert, but replaces.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Maybe if you use a "QLinkedList":http://qt-project.org/doc/qt-4.8/qlinkedlist.html of QByteArrays to store all the chunks and then at the end iterate over the list and write all the chunks to the file?

          QLinkedList has a method "prepend":http://qt-project.org/doc/qt-4.8/qlinkedlist.html#prepend which lets you insert items at the start of the list, also "insert":http://qt-project.org/doc/qt-4.8/qlinkedlist.html#insert can be used to insert an item before the first item I think.

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

            Using a buffer is the way to go, I think. Write out your data blocks to a QBuffer, and at the end, write out the header followed by the contents of the QBuffer to the actual file.

            Or, if you can, change your file format so you can stream in your data block by block.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goocreations
              wrote on last edited by
              #6

              The problem is that my audio files are 100mb - 200mb and I don't want to use that much memory for a "simple" task like this.

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

                Problem is: the task is not simple. Prepending to files (which is what you want to do) isn't supported by any file system I know. At least, not that I know of. The only thing you can do, is buffer. You can buffer in memory with something like QBuffer, or on disk (lower memory usage, but much higher IO costs), but buffering is the only option I see for your current format.

                Again: if you can, considder if you can change your format to something that does not have a variable-sized header that forces you to know the whole rest of the file before writing it.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  Is it absolutely necessary that there is only one pass involved in saving all this data?

                  You could write the blocks of raw encoded data out to the disk to a temporary file. You could have a second temporary file which contains records of the header data (perhaps with audio data offset metadata from the first file). After encoding is done, you could sequentially parse both temporary files and merge them into the completed audio file you need.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goocreations
                    wrote on last edited by
                    #9

                    Yip, I'll can write it out to a temp file and create a new file at the end. I just hoped I could be done more efficiently.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #10

                      I think for your scenario, that's probably about as efficient as you can hope for. There are always trade-offs in these kind of situations (size vs speed, etc..)

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      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