Nominate our 2022 Qt Champions!

How to edit/save a text file on a specific line (or lines) overwriting anything that may have been there before?

  • I'm using the following code to open up some files (using a for loop; hence the "G%1" filename, where %1 is the counter for how many times the loop has been run) and read only the lines between two tokens; QTRead and QTEnd, this can be any number of lines from zero to infinity, usually it will be only one or two lines though. See the following code to see how I'm doing it.

            QString Filename = QString(tr("/usr/share/g15daemon/macros/G%1")).arg(GSTATE);
            QString Menuname = QString(tr("G%1_M3_%2")).arg(GSTATE).arg("Dn");
            QString Output, startToken = "#QtRead", endToken="#QtEnd";
            QFile File(Filename);
            if (
                bool readEnabled = false;
                QTextStream M3_in(&File);
                while (!M3_in.atEnd())
                    QString M3line = M3_in.readLine();
                    if (M3line == endToken)
                        readEnabled = false;
                    if (readEnabled)
                        Output.append(M3line + "\n");
                    if (M3line == startToken)
                        readEnabled = true;

    And after this bit I just add in the code for outputting what is read between the tokens into the UI using a switch;

                    case 1:
                    case 2:
                    case 3:

    Might be a bit crude, but it works; however I also need to be able to do the opposite, and write whatever is in the UI down to the appropriate file at the appropriate position with a save button. How do I do this? for example if I want to save ui->G1_M3_Dn->toPlainText() to a file like /usr/share/g15daemon/G1 between the tokens #QT_Read and #QT_End overwriting whatever may have been between them before, and not affecting the rest of the file in any way?

  • read your file line by line and copy the lines to a temp file until you reach your token. Append the text from your ui to the temp file and read your file without appending until you reach your end token. Append all following lines to your temp file. delete your file and rename the temp file.

  • Thanks, I understand the concept but could I have a code example? I'm really a huge noob with Qt, and I've never really gotten that far with file handling in any language before either.

  • Quick and dirty. Misses error checking and will produce nonsense if the file has more than one #QT_Read or #QT_End, or if the tokens are in the wrong order.

    void changeFile(const QString &fileNameAbsolutePath, const QString &textToInsert)
        QFile originalFile(fileNameAbsolutePath);
        QFileInfo orgFlInfo(fileNameAbsolutePath);
        QFile tempFile(orgFlInfo.dir().absolutePath().append("/temp"));
        bool writeOriginalLine = true;
    | QIODevice::Text); | QIODevice::Text);
        if(originalFile.isOpen() && tempFile.isOpen())
            QTextStream org(&originalFile);
            QTextStream tmp(&tempFile);
                QString actualLine = org.readLine();
                if(writeOriginalLine) tmp << actualLine << "\n";
                if(actualLine == "#QT_Read")
                    writeOriginalLine = false;
                    tmp << textToInsert << "\n";
                else if(actualLine == "#QT_End")
                    writeOriginalLine = true;
                    tmp << actualLine << "\n";

Log in to reply