QFile write to front

  • 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).

  • 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?

  • 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.

  • 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.

  • 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.

  • 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.

  • 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.

  • Moderators

    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.

  • 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.

  • Moderators

    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..)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.