Results 1 to 3 of 3

Thread: reading from file

  1. #1
    Join Date
    Dec 2010
    Location
    Lithuania
    Posts
    29
    Qt products
    Qt4
    Platforms
    Windows

    Default reading from file

    So, I am trying to read form .txt file. While I am doing it I try to search for match of lineEdit value.
    Qt Code:
    1. QFile file(":/data/litEng.txt");
    2. file.open(QFile::ReadOnly);
    3. if (!file.isOpen())
    4. {
    5. qDebug() << "error";
    6.  
    7. }
    8. QTextStream stream ( &file );
    9. QString line;
    10. while( !stream.atEnd())
    11. {
    12. line = stream.readLine();
    13. if(line == wordEdit->text())
    14. {
    15. qDebug() << "ok";
    16. }
    17. qDebug() << "bad";
    18. }
    19. file.close();
    To copy to clipboard, switch view to plain text mode 

    but it says bad. Maybe I shouldn't use String for reading?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: reading from file

    It only means that line is never equal to wordEdit->text()

    Be careful with extra characters being read from the file. Spaces for example.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Talking Re: reading from file

    Qt Code:
    1. if(line == wordEdit->text())
    2. {
    3. qDebug() << "ok";
    4. }
    5. qDebug() << "bad";
    To copy to clipboard, switch view to plain text mode 
    Maybe better would be:
    Qt Code:
    1. f(line == wordEdit->text()){
    2. qDebug() << "ok";
    3. } else{
    4. qDebug() << "bad: " << line << ";" << wordEdit->text() << ";";
    5. }
    To copy to clipboard, switch view to plain text mode 
    This way you know why its "bad" (and dont print "bad" after its "ok" ).

Similar Threads

  1. Reading XML file using DOM
    By rk0747 in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2010, 22:09
  2. reading a file
    By xaqt in forum Newbie
    Replies: 6
    Last Post: 14th September 2009, 15:04
  3. reading xml file
    By dreamer in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2008, 02:01
  4. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55
  5. reading from a file
    By mickey in forum General Programming
    Replies: 32
    Last Post: 19th July 2007, 01:04

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
  •  
Qt is a trademark of The Qt Company.