PDA

View Full Version : finding a string in text file



sammey_geek
21st August 2013, 14:45
hi,

i am newbie.... i am working on qfile....i am having a file and some text data in it....i have to read the file till the end...and compare a string in that file..for example.. i have this data in file " this file contains properties and details of the project"..... in this i have to compare string properties....can anyone tell me..how to compare string...thanks in advance...

wysota
21st August 2013, 15:24
Is this a school project?

8Observer8
21st August 2013, 20:01
I hope, it is useful for beginning:



#include <QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString filePath = "C:/text.txt";

QFile file(filePath);

if (file.open(QFile::ReadOnly | QFile::Text)) {
QTextStream in(&file);
QString text = in.readAll();
qDebug() << text;
file.close();
}
else {
qDebug() << "Failed to open file for reading";
}

return a.exec();
}

sammey_geek
24th August 2013, 09:05
hi

how can i find a particular string in a .txt file?? is there any qt function to search the .txt file and find the string??






thanks
sammey

wysota
24th August 2013, 09:13
http://en.wikipedia.org/wiki/String_searching_algorithm

If you can afford reading the whole file to memory before searching, you can load the file into a string and then use QString::indexOf() or similar.