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.