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. Search and edit text file for string
Qt 6.11 is out! See what's new in the release blog

Search and edit text file for string

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.5k 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.
  • C Offline
    C Offline
    cruzerguy
    wrote on last edited by
    #1

    I am new to Qt and this is my first application.

    I need to search for a string in a text file and write another text file with the line that includes the search string and everything following it. I wrote the following code, but the search doesn't seem to be working. The new text file is blank because a match is not found. I would like to know what's wrong with the search.
    @
    int counter = 0;
    int linenumb = 0;
    QString search = " search string";
    QFile tempfile("C:\test\temp.textf");
    QFile workingdoc("C:\test\workingdoc.txt");
    tempfile.open(QIODevice::ReadOnly|QIODevice::Text);
    workingdoc.open(QIODevice::WriteOnly|QIODevice::Text);
    QTextStream in(&tempfile);
    QTextStream out(&workingdoc);
    while(!in.atEnd())
    {
    linenumb++;
    QString line = in.readLine();
    if (line == search)
    {break;}
    }

    while(!in.atEnd())
    {
    counter ++;
    QString line = in.readLine();
    if (counter >= linenumb)
    {out<<line<<endl;}
    }

    tempfile.close();
    workingdoc.close();
    

    }
    @
    [andreyc EDIT]: please use @ around code

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      Welcome to DevNet

      Let say in a file C:\test\temp.textf you have
      @
      line1
      line2
      line3
      @

      and search string is "line2"

      What do you expect as a result? Is it like this:
      @
      line2
      line1
      line2
      line3
      @

      If so then, you did not restart input stream between two loops, so your second loop continues to a read a file from the place where first one stopped.
      And I don't see where you write your "search string" to a file.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cruzerguy
        wrote on last edited by
        #3

        thanks andreyc.

        that's what I would like to write. I would like the new file to contain

        line 2
        line 3

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          Then you don't need counters.
          Just read from one file until you hit the search line and start to write after that. Something like this
          @
          QString line;
          while(!in.atEnd())
          {
          line = in.readLine();
          if (line == search)
          {
          found = true;
          out<<line<<endl;
          break;
          }
          }

          while(found && !in.atEnd())
          {
          line = in.readLine();
          out<<line<<endl;
          }
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cruzerguy
            wrote on last edited by
            #5

            thanks for your help andreyc.

            I fixed it, turns out it was the greater than or equal to operator. if you change the >= to <= in line 24 of my post, it works. :)

            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 Extensions
            • Unsolved