Reading/Editing data in a QTextStream



  • 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?


  • Lifetime Qt Champion

    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.



  • @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.


  • Lifetime Qt Champion

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



  • @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?


  • Lifetime Qt Champion

    The QTextSteam::seek method comes to mind.



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



  • 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()



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.