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:
13 replies
It depends :-)
If you know, it’s xml, you can read it by SAX or DOM or other methods (see QtXml [doc.qt.nokia.com] or QtXmlPatterns [doc.qt.nokia.com] ) and the work on the read data.
The other posibility is to read with QTextStream [doc.qt.nokia.com].
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
If you look at the given links (QTextStream [doc.qt.nokia.com] ) there is a description, how to read a file to a QString.
you can also read using QFile directly. From the docu:
- return;
- while (!in.atEnd()) {
- process_line(line);
- }
or
- return;
- while (!file.atEnd()) {
- process_line(line);
- }
or
- return;
For more information on that, I propose a look at the Qt documentation (link in the top right corner, called DOC) :-)
Can you tell me how to convert whole file into QTextStream?
I’ve done something like this (after closing file):
- 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?
First method (SAX, DOM) doesn’t work. Example programs gives error (unknown char) … ?What type does your data has? is it XML? is it plain text?
I downloaded page with:
- 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.
Can you tell me how to convert whole file into QTextStream?You can first convert QFile into QString, example:
I’ve done something like this (after closing file):
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?
- QString item;
- char buff[255];
- while (!file.atEnd())
- {
- file.readLine(buff, sizeof(buff));
- item.append(buff);
- }
and QString you can convert to QTextStream
or you can even in QString find. You have function indexOF [doc.qt.nokia.com] , who returns you position of looking word.
You can first convert QFile into QString, example:
@QFile file(plik); QString item; file.open(QFile::ReadOnly); char buff255; while (!file.atEnd()) { file.readLine(buff, sizeof(buff)); item.append(buff); }@
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…
return;Then use QString functions
look at the docu, it is really good
something with find/search/indexOf…
If you read all posts, you perhaps find the solution :-)
QString::indexOf [doc.qt.nokia.com]
You must log in to post a reply. Not a member yet? Register here!




