Results 1 to 5 of 5

Thread: finding a string in text file

  1. #1
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default comparing a string in a text file

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: comparing a string in a text file

    Is this a school project?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Location
    Russia
    Posts
    228
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: comparing a string in a text file

    I hope, it is useful for beginning:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QFile>
    3. #include <QTextStream>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. QString filePath = "C:/text.txt";
    11.  
    12. QFile file(filePath);
    13.  
    14. if (file.open(QFile::ReadOnly | QFile::Text)) {
    15. QTextStream in(&file);
    16. QString text = in.readAll();
    17. qDebug() << text;
    18. file.close();
    19. }
    20. else {
    21. qDebug() << "Failed to open file for reading";
    22. }
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2013
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default finding a string in text file

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: finding a string in text file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QTextEdit - Finding text within selection
    By kringen in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2012, 09:41
  2. Replies: 3
    Last Post: 8th June 2011, 07:36
  3. Problem with Finding the Text
    By charlesprime in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2011, 11:10
  4. Finding text on Text edit
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 18th May 2006, 14:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.