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. Appending and reading from the end of a file.
Forum Updated to NodeBB v4.3 + New Features

Appending and reading from the end of a file.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.3k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dulajun
    wrote on last edited by
    #1

    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”);
    myFile.open(QIODevice::Append);
    QDataStream out(&myFile);
    //Here the code
    …
    //read from the end 1024 letters
    myFile.open(QIODevice::ReadOnly);
    QDataStream in(&myFile);
    QString text; //to receive the text.
    //Here the code

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      What do you mean by 1024 unicode characters? Are you talking about codepoints (basically one entry in those nice tables available at unicode.org), 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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dulajun
        wrote on last edited by
        #3

        I meant a specific amount of data in bytes.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dulajun
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                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":http://www.flexhex.com/docs/articles/alternate-streams.phtml

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                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