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. [Solved] reading and writing the same file while replacing a string
Forum Update on Monday, May 27th 2025

[Solved] reading and writing the same file while replacing a string

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 12.9k 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.
  • M Offline
    M Offline
    mr_maddog
    wrote on 21 Mar 2013, 10:29 last edited by
    #1

    Hi,

    What is wrong here?
    I try to open a file, read in and while reading replace a string with another. fileName is a qstring which i got from QFileDialog
    finally write out the file with the same name than the loaded.

    @
    QFile PerlFile("/fs1/PerlQt/QtCreator/EngineMountStiffness/test.pl");
    PerlFile.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream in(&PerlFile);
    QString line = in.readAll();
    QString ListPerlFile = line.replace(QString("PchFile"), fileName);
    in << ListPerlFile;
    PerlFile.close();
    @

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on 21 Mar 2013, 10:46 last edited by
      #2

      I don't know if you expect the files content to be changed? If so what do you think does QIODevice::ReadOnly mean. ;-)

      BTW you cannot just replace content in a text file, you can only append content. You have to read the file content (as you already do) delete the file and create a new file with the same name and the new content.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CAD_coding
        wrote on 21 Mar 2013, 10:47 last edited by
        #3

        change that to ReadWrite.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Mar 2013, 11:09 last edited by
          #4

          Hi,

          I have to correct KA51O. You can make change in a file "in place" you have to open it (ReadWrite or WriteOnly) and then seek to the right position and write from that point. Seeking then writing will overwrite the current content. Append will happen only at the end of the file and if the option is given to open().

          In your case and if test.pl is not to big i would rather do something like:
          @
          perlFile.open(QIODevice::ReadWrite|QIODevice::Text);
          QByteArray fileContent = perlFile.readAll();
          fileContent.replace(QString("PchFile"), fileName);
          perlFile.seek(0);
          perlFile.write(fileContent);
          perlFile.close();
          @

          Warning: code not tested

          Answer edited

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on 21 Mar 2013, 12:02 last edited by
            #5

            [quote author="SGaist" date="1363864179"]Hi,

            I have to correct KA51O. You can make change in a file "in place" you have to open it (ReadWrite or WriteOnly) and then seek to the right position and write from that point. That will either overwrite the current content or append depending on the option given to open().

            In your case and if test.pl is not to big i would rather do something like:
            @
            perlFile.open(QIODevice::ReadWrite|QIODevice::Text);
            QByteArray fileContent = perlFile.readAll();
            fileContent.replace(QString("PchFile"), fileName);
            perlFile.seek(0);
            perlFile.write(fileContent);
            perlFile.close();
            @

            Warning: code not tested[/quote]

            Thanks for the correction. One more question. I thought that with using seek you can only overwrite from that position on and that append is only possible at the end of the file?

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on 21 Mar 2013, 12:10 last edited by
              #6

              Why fragment the file.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 21 Mar 2013, 12:15 last edited by
                #7

                @KA510, you're right, i wasn't clear. I updated my answer.

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mr_maddog
                  wrote on 21 Mar 2013, 13:12 last edited by
                  #8

                  This is what i wanted to do.
                  somebody have a nicer one?

                  Thanks to u all

                  @QFile PerlFile("/na/share/LINUX/public/PuZ_Bench/test.pl");
                  QFile outPerlFile("/na/share/LINUX/public/PuZ_Bench/out.pl");
                  PerlFile.open(QIODevice::ReadOnly|QIODevice::Text);
                  outPerlFile.open(QIODevice::WriteOnly|QIODevice::Text);
                  QTextStream in(&PerlFile);
                  QTextStream out(&outPerlFile);
                  while (!in.atEnd()) {
                  QString line = in.readLine();
                  QString outline = line.replace(QString("PchFile"), fileName);
                  out << outline << "\n";
                  }

                      PerlFile.close();
                      outPerlFile.close();
                  

                  @

                  1 Reply Last reply
                  0

                  1/8

                  21 Mar 2013, 10:29

                  • Login

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