Important: Please read the Qt Code of Conduct -

Appending and reading from the end of a file.

  • Could any one help me in modifying this snippet of code or rewriting from scratch, to append a text to the end of the file and then read that text from the end of the same file. The text has a length of 1024 unicode characters exactly.

    // append 1024 letters
    QFile myFile(”/Users/dulajun/Documents/pages.indd”);;
    QDataStream out(&myFile);
    //Here the code

    //read from the end 1024 letters;
    QDataStream in(&myFile);
    QString text; //to receive the text.
    //Here the code

  • Moderators

    What do you mean by 1024 unicode characters? Are you talking about codepoints (basically one entry in those nice tables available at, or maybe glyphs (things painted, may consist of one or more codepoints) or simply 16bit integer values as e.g. stored in QChar (there are more than 2^16 codepoints, so you might only get part of one codepoint in one of these values)?

    Plus depending on the encoding you use you will get a different number of bytes for your 1024 characters. The length will not only depend on the encoding but also on the language the characters are from.

    Yeap, unicode is interesting;-)

    QDataStream will add meta data of its own, so you can not just read the last 1024*x bytes to retrieve your data.

    The simplest approach might be to just write 1024 16bit values (== the first 1024 characters of a QString) into the file. You can get the data using QString::utf16 (and maybe a cast;).

    Retrieving those is just a matter of reading the last 2048bytes and turning them into a QString again using QString::fromUtf16.

    If you need a valid unicode string then replace the last QChar if it isHighSurrogate() to avoid having only half a surrogate pair (these pairs are used to encode the codepoints that do not fit into 16bit) at the end of the string when reading it back.

  • I meant a specific amount of data in bytes.

  • Lifetime Qt Champion


    Just to be sure (since this thread seems related to the other one you posted), you do realize that appending data at end of random files can end up in that files being considered as corrupted ?

  • So, is there a specific location in the file despite it's format for adding custom metadata to use in synchronising and file's links tracing?

    What I want is to stick data to the files to go any where the file go.

  • Lifetime Qt Champion

    No, for example: txt files, csv files, executables and many others don't have any place for meta-data. That's not the job of the files to contains such informations. Rather the filesystem or an external service like Nepomuk.

  • As explained before, if you want store Metadata inside the file, this must happen in a way specific to the individual file format. In other words: It must be done in a different way for each type of file - which will not be trivial at all, if you want to support various different file formats.

    And, SGaist said, there are many file types which simply don't have a "well defined" way to include Metadata. You cannot simply append your data to the file in this case. It may not always cause problems immediately, but it definitely is not a good idea. It may cause unexpected problems in the future use of the file.

    While I think storing your Metadata in a separate database, not inside the files, is strongly preferable, there still is one method that comes to my mind. This method its specific to Windows and to the NTFS file system:

    "NTFS Alternate Streams":

Log in to reply