Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Appending and reading from the end of a file.

    General and Desktop
    4
    7
    3733
    Loading More Posts
    • 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
      dulajun last edited by

      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 Reply Quote 0
      • T
        tobias.hunger last edited by

        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 Reply Quote 0
        • D
          dulajun last edited by

          I meant a specific amount of data in bytes.

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • D
              dulajun last edited by

              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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • M
                  MuldeR last edited by

                  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 Reply Quote 0
                  • First post
                    Last post