Qt Forum

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

    Unsolved Reading/Editing data in a QTextStream

    QML and Qt Quick
    4
    9
    749
    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.
    • A
      amanill last edited by

      Hello, so I need to verify data being read from a file to check if specific data is at known location

      My TextStream is created as such:

              QByteArray ba;
              QTextStream ts(&ba, QIODevice::ReadWrite);
      
              filea.open(QIODevice::ReadOnly | QIODevice::Text);
              fileb.open(QIODevice::ReadOnly | QIODevice::Text);
      
              ts << filea.readAll();
              ts << fileb.readAll();
      

      How can i go about reading / editing the information in the TextStream or ByteArray?

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

        Hi,

        Since you call readAll why do you want to use QTextStream at all ? You already get a QByteArray from that method.

        On a side note, you don't verify that filea nor fileb are successfully opened.

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

        A 1 Reply Last reply Reply Quote 1
        • A
          amanill @SGaist last edited by

          @SGaist Hi, those were just bits of my code, let me explain what exactly i'm building, I need to take 2 hex files, check certain sections of the hex files to see if they contain specific sets of data, if they do I need to rearrange or insert segments of code, after which the two hex files are merged and then run through a TEA encrypt and outputting the encrypted file. I've managed to get everything working except for inspecting/editing the segments.

          Ultimately I was given an excel spreadsheet that uses a ton of macros, and they wanted me to build an exe to replace it.

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

            What exactly is failing ?
            Did you add the checks for your QFile::open calls?

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

            A 1 Reply Last reply Reply Quote 0
            • A
              amanill @SGaist last edited by

              @SGaist nothing is failing, I just don't know how to read into the textstream at a specific location, it's not just treated like an array is it?

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

                The QTextSteam::seek method comes to mind.

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

                A 1 Reply Last reply Reply Quote 0
                • A
                  amanill @SGaist last edited by

                  @SGaist documentation says that returns a bool, and not the value, but I'll give it a shot

                  JonB 1 Reply Last reply Reply Quote 0
                  • 6thC
                    6thC last edited by 6thC

                    seek: Seeks to the position pos in the device.

                    so, it does that on the device - it just also returns true or false so you can (assumedly) check.

                    quick google came up with writing to a predefined pos: (where the result is being discarded)
                    http://www.qtcentre.org/threads/3836-QFile-QTextStream-seek()

                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB @amanill last edited by

                      @amanill said in Reading/Editing data in a QTextStream:

                      @SGaist documentation says that [QTextStream::seek] returns a bool, and not the value, but I'll give it a shot

                      What "value"? The seek just returns a bool to say whether the seek has been successful, to read what bytes are there you then follow that with some normal "read bytes" operation, it's just that the seek has moved you instantaneously to a particular position in the file.

                      If you then want to write into the file at that point. Firstly you'd have to have opened the file for read/write, not just read. Secondly, unless you know you will only ever overwrite bytes and never insert or delete bytes, you will probably find you need to rewrite the whole file from start to finish, either by pre-reading its entire content into memory before starting to write or by reading from one file while writing to a brand new one and cleaning up on finish. You cannot insert or delete into/from the middle of a file and have it extend/contract to accommodate your updates.

                      1 Reply Last reply Reply Quote 2
                      • First post
                        Last post