Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Reading/Editing data in a QTextStream
QtWS25 Last Chance

Reading/Editing data in a QTextStream

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.3k Views
  • 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 Offline
    A Offline
    amanill
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • SGaistS SGaist

        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.

        A Offline
        A Offline
        amanill
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • SGaistS SGaist

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

            A Offline
            A Offline
            amanill
            wrote on last edited by
            #5

            @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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • SGaistS SGaist

                The QTextSteam::seek method comes to mind.

                A Offline
                A Offline
                amanill
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • 6thC6 Offline
                  6thC6 Offline
                  6thC
                  wrote on last edited by 6thC
                  #8

                  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
                  0
                  • A amanill

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @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
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved