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. Searching in QFile which has source of webpage
Qt 6.11 is out! See what's new in the release blog

Searching in QFile which has source of webpage

Scheduled Pinned Locked Moved General and Desktop
14 Posts 5 Posters 8.2k 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.
  • R Offline
    R Offline
    roosaw
    wrote on last edited by
    #1

    I have a source of the page which is downloaded by get method from QHttp and i wanna search a some phrase in it. How to do that?
    Secondly, when i will search it i wanna go forward with a few chars then i wanna put what i got in double type and put it in a variable. How to do that?

    I downloaded a page with something like that:
    @file= new QFile("plik.xml", this);
    file->open(QIODevice::ReadWrite);
    http = new QHttp(this);
    http->setHost("addressofpage.com");
    http->get("/pathtopage.html", file);
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      It depends :-)

      If you know, it's xml, you can read it by SAX or DOM or other methods (see "QtXml":http://doc.qt.nokia.com/latest/qtxml.html or "QtXmlPatterns":http://doc.qt.nokia.com/latest/qtxmlpatterns.html ) and the work on the read data.

      The other posibility is to read with "QTextStream":http://doc.qt.nokia.com/latest/qtextstream.html.

      Examples to that can be found in the examples section of the documentation.

      If it's not xml to parse, I would read into a QString and search/insert there

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roosaw
        wrote on last edited by
        #3

        First method (SAX, DOM) doesn't work. Example programs gives error (unknown char)
        How to read to a QString?

        QString str;
        str << file;
        ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          CreMindES
          wrote on last edited by
          #4

          Gerof is right I think, I would use QTextStream too...

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            If you look at the given links ("QTextStream":http://doc.qt.nokia.com/latest/qtextstream.html#details ) there is a description, how to read a file to a QString.

            you can also read using QFile directly. From the docu:

            @
            QFile file("in.txt");
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;

            QTextStream in(&file);
            while (!in.atEnd()) {
                QString line = in.readLine();
                process_line(line);
            }
            

            @

            or

            @
            QFile file("in.txt");
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;

             while (!file.atEnd()) {
                 QByteArray line = file.readLine();
                 process_line(line);
             }
            

            @

            or

            @
            QFile file("in.txt");
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            return;

            QTextStream in(&file);
            QString str = in.readAll();
            

            @

            For more information on that, I propose a look at the Qt documentation (link in the top right corner, called DOC) :-)

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              [quote author="roosaw" date="1293738563"]First method (SAX, DOM) doesn't work. Example programs gives error (unknown char)
              ...
              ?[/quote]

              What type does your data has? is it XML? is it plain text?

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • R Offline
                R Offline
                roosaw
                wrote on last edited by
                #7

                Can you tell me how to convert whole file into QTextStream?
                I've done something like this (after closing file):

                @QFile file("plik.xml");
                if (file.open(QFile::ReadWrite)) {
                QTextStream out(&plik);
                out.readAll();
                // out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
                // writes "Result: 3.14 2.7 "
                }
                file.close();@

                From documentation i know that it convert out to QString. Now how can i seek a phrase in there?

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  roosaw
                  wrote on last edited by
                  #8

                  [quote author="Gerolf" date="1293739383"]
                  [quote author="roosaw" date="1293738563"]First method (SAX, DOM) doesn't work. Example programs gives error (unknown char)
                  ...
                  ?[/quote]

                  What type does your data has? is it XML? is it plain text?[/quote]

                  I downloaded page with:
                  @ file= new QFile("plik.xml", this);
                  file->open(QIODevice::ReadWrite);
                  http = new QHttp(this);
                  http->setHost("addressofpage.com");
                  http->get("/pathtopage.html", file);@
                  but i can look at it in text editor so... i think it is a plain text.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

                    @
                    QFile file("in.txt");
                    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                    return;

                    QTextStream in(&file);
                    QString str = in.readAll();
                    @

                    Then use QString functions

                    look at the docu, it is really good

                    something with find/search/indexOf...

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BlackDante
                      wrote on last edited by
                      #10

                      [quote author="roosaw" date="1293739634"]Can you tell me how to convert whole file into QTextStream?
                      I've done something like this (after closing file):

                      @QFile file("plik.xml");
                      if (file.open(QFile::ReadWrite)) {
                      QTextStream out(&plik);
                      out.readAll();
                      // out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
                      // writes "Result: 3.14 2.7 "
                      }
                      file.close();@

                      From documentation i know that it convert out to QString. Now how can i seek a phrase in there?
                      [/quote]
                      You can first convert QFile into QString, example:
                      @QFile file(plik);
                      QString item;
                      file.open(QFile::ReadOnly);
                      char buff[255];
                      while (!file.atEnd())
                      {
                      file.readLine(buff, sizeof(buff));
                      item.append(buff);
                      }@
                      and QString you can convert to QTextStream
                      @QTextStream text = new QTextStream(item,QIODevice::ReadOnly);@

                      or you can even in QString find. You have function "indexOF":http://doc.qt.nokia.com/4.7/qstring.html#indexOf , who returns you position of looking word.

                      sorry for my broken english :)

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        First read the file into a QString, change the QString and write the string back to the file.

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          roosaw
                          wrote on last edited by
                          #12

                          [quote author="BlackDante" date="1293741181"]
                          You can first convert QFile into QString, example:
                          @QFile file(plik);
                          QString item;
                          file.open(QFile::ReadOnly);
                          char buff[255];
                          while (!file.atEnd())
                          {
                          file.readLine(buff, sizeof(buff));
                          item.append(buff);
                          }@[/quote]
                          Ok, it works :) I mean... compiling...
                          Now how can i search specific string in that QString (someting like item.seek("I/'m seeking this string");
                          Is there is something like this?
                          I found that:
                          http://doc.qt.nokia.com/latest/qstring.html#compare-5
                          but it compare only lenght of two strings...

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            [quote author="Gerolf" date="1293741140"]@
                            QFile file("in.txt");
                            if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
                            return;

                            QTextStream in(&file);
                            QString str = in.readAll();
                            @

                            Then use QString functions

                            look at the docu, it is really good

                            something with find/search/indexOf...[/quote]

                            If you read all posts, you perhaps find the solution :-)

                            "QString::indexOf":http://doc.qt.nokia.com/latest/qstring.html#indexOf

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              If you want to read the complete file into a string readAll() is your friend:

                              @
                              QFile file("mytextfile.txt");
                              if(!file.open(QIODevice::ReadOnly))
                              qFatal() << "File cannot be opened";

                              QTextStream ts(file);
                              QString fileContent = ts.readAll();
                              file.close();
                              @

                              If you read strings from a file it's always better to use QTextStream, as it does the proper decoding (UTF-8 etc.) for you.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              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