December 30, 2010

roosaw roosaw
Lab Rat
16 posts

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:

  1. file= new QFile("plik.xml", this);
  2.         file->open(QIODevice::ReadWrite);
  3.         http = new QHttp(this);
  4.         http->setHost("addressofpage.com");
  5.         http->get("/pathtopage.html", file);

13 replies

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts

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

 Signature 

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

December 30, 2010

roosaw roosaw
Lab Rat
16 posts

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

QString str;
str << file;
?

December 30, 2010

CreMindES CreMindES
Lab Rat
211 posts

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

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts

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:

  1.     QFile file("in.txt");
  2.     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3.         return;
  4.  
  5.     QTextStream in(&file);
  6.     while (!in.atEnd()) {
  7.         QString line = in.readLine();
  8.         process_line(line);
  9.     }

or

  1.      QFile file("in.txt");
  2.      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3.          return;
  4.  
  5.      while (!file.atEnd()) {
  6.          QByteArray line = file.readLine();
  7.          process_line(line);
  8.      }

or

  1.      QFile file("in.txt");
  2.      if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3.          return;
  4.  
  5.     QTextStream in(&file);
  6.     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) :-)

 Signature 

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

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts
roosaw wrote:
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?

 Signature 

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

December 30, 2010

roosaw roosaw
Lab Rat
16 posts

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

  1. QFile file("plik.xml");
  2.  if (file.open(QFile::ReadWrite)) {
  3.      QTextStream out(&plik);
  4.      out.readAll();
  5.      // out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
  6.      // writes "Result: 3.14      2.7       "
  7.  }
  8. file.close();

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

December 30, 2010

roosaw roosaw
Lab Rat
16 posts
Gerolf wrote:
roosaw wrote:
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:

  1.  file= new QFile("plik.xml", this);
  2.         file->open(QIODevice::ReadWrite);
  3.         http = new QHttp(this);
  4.         http->setHost("addressofpage.com");
  5.         http->get("/pathtopage.html", file);

but i can look at it in text editor so… i think it is a plain text.

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts

  1. QFile file("in.txt");
  2.  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3.      return;
  4.  
  5. QTextStream in(&file);
  6. QString str = in.readAll();

Then use QString functions

look at the docu, it is really good

something with find/search/indexOf…

 Signature 

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

December 30, 2010

BlackDante BlackDante
Lab Rat
55 posts

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

  1. QFile file("plik.xml");
  2.  if (file.open(QFile::ReadWrite)) {
  3.      QTextStream out(&plik);
  4.      out.readAll();
  5.      // out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7;
  6.      // writes "Result: 3.14      2.7       "
  7.  }
  8. file.close();

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

You can first convert QFile into QString, example:
  1. QFile file(plik);
  2.    QString item;
  3.     file.open(QFile::ReadOnly);
  4.     char buff[255];
  5.     while (!file.atEnd())
  6.     {
  7.         file.readLine(buff, sizeof(buff));
  8.         item.append(buff);
  9.     }

and QString you can convert to QTextStream
  1. QTextStream text = new QTextStream(item,QIODevice::ReadOnly);

or you can even in QString find. You have function indexOF [doc.qt.nokia.com] , who returns you position of looking word.

 Signature 

sorry for my broken english :)

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts

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

 Signature 

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

December 30, 2010

roosaw roosaw
Lab Rat
16 posts

BlackDante wrote:
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…

December 30, 2010

Gerolf Gerolf
Robot Herder
3252 posts

Gerolf wrote:
  1. QFile file("in.txt");
  2.  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  3.      return;
  4.  
  5. QTextStream in(&file);
  6. QString str = in.readAll();

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]

 Signature 

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

December 31, 2010

Volker Volker
Ant Farmer
5428 posts

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

  1. QFile file("mytextfile.txt");
  2. if(!file.open(QIODevice::ReadOnly))
  3.   qFatal() << "File cannot be opened";
  4.  
  5. QTextStream ts(file);
  6. QString fileContent = ts.readAll();
  7. 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.

 
  ‹‹ Internationalization and QDate::toString      undefined reference to ››

You must log in to post a reply. Not a member yet? Register here!