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. Issues modifying QFile information
Forum Updated to NodeBB v4.3 + New Features

Issues modifying QFile information

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 389 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, I'm trying to open a QFile that contains lines of data that represent intel hex and make some changes,

    every line starts with a colon, and I am trying to replaced the colon with 0x

    here is what I have, which seems to result in an empty file from what I can tell based on my output file:

    QFile filea(file1);
    QFile fileb(file2);
    QFile result(loc);
        if(filea.open(QIODevice::ReadWrite) && fileb.open(QIODevice::ReadWrite) && result.open(QIODevice::ReadWrite)){
            //process the filea
            QTextStream in1(&filea);
            while(!in1.atEnd()){
                QString line = in1.readLine();
                line.remove(0,1);
                line.prepend("0x");
                qDebug() << line;
                in1<< line <<endl;
            }
             QByteArray ba;
            QTextStream ts(&ba, QIODevice::ReadWrite);
    
            // merge files into textstream
            ts << filea.readAll();
            ts << fileb.readAll();
            result.write(ba);
    

    Am i missing something where i push my QTextStream in1 back into filea?

    JonBJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      This should not create the empty file. It should append the content at the end of the file. Looks like you did not close the filea handle once taks is over. Just try filea.close() and see what happens.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • A amanill

        Hello, I'm trying to open a QFile that contains lines of data that represent intel hex and make some changes,

        every line starts with a colon, and I am trying to replaced the colon with 0x

        here is what I have, which seems to result in an empty file from what I can tell based on my output file:

        QFile filea(file1);
        QFile fileb(file2);
        QFile result(loc);
            if(filea.open(QIODevice::ReadWrite) && fileb.open(QIODevice::ReadWrite) && result.open(QIODevice::ReadWrite)){
                //process the filea
                QTextStream in1(&filea);
                while(!in1.atEnd()){
                    QString line = in1.readLine();
                    line.remove(0,1);
                    line.prepend("0x");
                    qDebug() << line;
                    in1<< line <<endl;
                }
                 QByteArray ba;
                QTextStream ts(&ba, QIODevice::ReadWrite);
        
                // merge files into textstream
                ts << filea.readAll();
                ts << fileb.readAll();
                result.write(ba);
        

        Am i missing something where i push my QTextStream in1 back into filea?

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

        @amanill

        Am i missing something where i push my QTextStream in1 back into filea?

        QString line = in1.readLine();
        in1<< line <<endl;
        

        I don't understand. You are reading a line from in1, and you are later writing a line into the same in1. You can't do that! (Well, you can, but it won't behave as I'm suspecting you might expect it to!).

        You are changing one character in each line to two characters. Therefore, if you think it through, you will realise you cannot update back to the same file as you are reading from it. You need to change to either of the following (each with pros & cons):

        1. Open the file for read. Read the whole file into memory. Close the file. Re-open the file for write. Output all the lines, with your changes. Close the file.

        2. Open the file for read. Open a different, temporary file (often in the same directory as the original) for write. Read lines from the first file, outputting the changed lines to the new file as you go. Close both files. Delete the original file, and rename the new file to the original file now.

        1 Reply Last reply
        3

        • Login

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