Searching in QFile which has source of webpage



  • 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);
    @



  • 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



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

    QString str;
    str << file;
    ?



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



  • 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) :-)



  • [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?



  • 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 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.



  • @
    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 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.



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



  • [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...



  • [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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.