Results 1 to 2 of 2

Thread: Nested loop only loops 3 times instead of 710 times

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Nested loop only loops 3 times instead of 710 times

    I am trying to implement something that resembles a "brute string search algorithm", only that I am matching strings instead of characters. I've got two files (i.e. file A and file B), the intention is to read the first line from file A and then parse the entire file B to see if that line is contained in file B. If the line is not contained in file B, the program must move on to line number 2 in file A and the process is repeated until the end of file A.
    Qt Code:
    1. void crossRefernce(QString input1, QString input2, QString output_file)
    2. {
    3. QFile file1(input1);
    4. QFile file2(input2);
    5. QFile outputFile(output_file);
    6.  
    7. if(!file1.open(QFile::ReadOnly | QFile::Text))
    8. {
    9. qDebug() << "file1 not opened";
    10. }
    11.  
    12. if(!file2.open(QFile::ReadOnly | QFile::Text))
    13. {
    14. qDebug() << "file2 not opened";
    15. }
    16.  
    17. if(!outputFile.open(QFile::WriteOnly | QFile::Text))
    18. {
    19. qDebug() << "output file not opened";
    20. }
    21.  
    22.  
    23. QTextStream in_file1(&file1);
    24. QTextStream in_file2(&file2);
    25. QTextStream out(&outputFile);
    26.  
    27.  
    28. while(!in_file1.atEnd())
    29. {
    30.  
    31. qDebug() << "Looping" <<endl;
    32. QString file1_str = in_file1.readLine(0);
    33. QString temp_file1_str = file1_str.mid(0, file1_str.indexOf('.'));
    34.  
    35. while(!in_file2.atEnd())
    36. {
    37. qDebug() << "**Inner Loop**" <<endl;
    38. QString file2_str = in_file2.readLine(0);
    39. QString temp_file2_str = file2_str.mid(0, file2_str.indexOf('.'));
    40.  
    41. if(temp_file2_str == temp_file1_str)
    42. {
    43. qDebug() << "found" <<endl;
    44. out << file2_str <<endl;
    45. qDebug() << temp_file2_str <<endl;
    46. file2_str.clear();
    47. break;
    48. }
    49. file2_str.clear();
    50. }
    51.  
    52.  
    53. file1_str.clear();
    54. }
    55.  
    56.  
    57. file1.close();
    58. file2.close();
    59. outputFile.close();
    60. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that, the output of the above code indicates that the inner while loop iterates only 3 times and never executes again. The ultimate goal is to delete similar entries in file A and B and leave unique values. Your help will be greatly appreciated.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Nested loop only loops 3 times instead of 710 times

    Did you hit a match on the 3rd time through the inner loop? Do you really want to stop searching the rest of file2 when you find a match? Your break statement on line 47 will fall out of that inner loop. Also, when file2 is at EOF, you don't do anything to reset it, so next time through your outer loop, the inner loop won't execute. See QTextSteam::seek() to reset the file at the beginning once you fall out of the inner loop.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. QRegExp Not Repeated More Than Four Times.
    By mandlakoteswar2011 in forum Qt Programming
    Replies: 8
    Last Post: 22nd September 2015, 21:34
  2. QResizeEvent called 3 times
    By mvbhavsar in forum Newbie
    Replies: 5
    Last Post: 17th December 2014, 16:06
  3. How to use a class multiple times?
    By xtlc in forum Newbie
    Replies: 5
    Last Post: 8th August 2013, 15:48
  4. Qwt 6.0.1 three times slower than 5.2.0
    By Spitfire in forum Qwt
    Replies: 13
    Last Post: 26th September 2011, 10:05
  5. how to calculate difference b/w two times
    By dummystories in forum Newbie
    Replies: 1
    Last Post: 9th March 2009, 13:58

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.