Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find and replace
Qt 6.12 LTS is out! See what's new in the release blog

Find and replace

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.7k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    @
    QString filename = "xxx.txt";
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    return;
    }
    QTextStream in(&file);
    QStringList line;
    while (!in.atEnd())
    {
    line.append(in.readLine());
    }
    QString lineedit =line.at(1);
    qDebug()<<lineedit;
    lineedit.replace("Vidhya","Nithya");
    qDebug()<<lineedit;

    file.close(); @
    

    Here the text is changed but it is not reflected in the file .
    how will i achive this.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      What do you think does this do ?
      @
      file.open(QIODevice::ReadOnly | QIODevice::Text)
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        i gave QIODevice::ReadWrite also but its not working

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          This part is replacing something in a QString variable (QString lineedit) and not in your QFile or QTextStream.
          @
          lineedit.replace("Vidhya","Nithya");
          @
          and you do not write the changes you did to this lineedit variable to your file or textstream anywhere.

          Also before posting a question like this, that is very likely to have been asked before, try "searching the forum":http://qt-project.org/search?search=replace+text+in+file for "existing threads":http://qt-project.org/forums/viewthread/6203 on this issue.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            David_Gil
            wrote on last edited by
            #5

            Hi!

            As KA510 says, you're opening the file as ReadOnly.

            Besides, your line

            @QTextStream in(&file);@

            could be better understood as

            @QTextStream reader(&file);@

            AFAIK (which is not much) what you need is something that 'writes' to that or another file:

            @QTextStream writer(&anotherFile);
            //...
            QString lineedit =line.at(1);
            lineedit.replace("Vidhya","Nithya");
            writer << lineedit;
            @

            I hope it (somewhat) helps!

            David Gil

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Thank for ur reply i achieved it by writing the result to the another file thank for ur help

              1 Reply Last reply
              0

              • Login

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