Search code examples
qtqfile

append text into the beginning of a textfile


Is there a way that I can always append new text into the beginning of a text file in Qt? i'm using QFile::Append to do it.

file.open(QFile::Append | QFile::Text)

Solution

  • You can't, see the documentation at http://doc.qt.io/qt-5/qiodevice.html:

    QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.

    The problem is even worse, a file is usually stored sequentially on disk, appending (better: inserting) at the start of a file would involve moving all data towards the end of the file, thus a reorganization of filesystem blocks. I'm not sure such a filesystem exists, but if, I guess it would only allow insertion of a multiple of the filesystem block size into a file.