Quote Originally Posted by D_Drmmr View Post
There is no need to parse the files twice if you just want to check that they have the same number of lines. laserlight's suggestion can be easily altered to do that.
Code:
bool res1, res2;
while((res1 = getline(read_file1, new_line1)) &&
      (res2 = getline(read_file2, new_line2)))
{
    // ...
}
if (res1 || res2)
{
    // number of lines does not match
}
This won't work due to the short-circuit evaluation of the && operator. Suppose we have files with equal numbers of lines. While we are reading lines in both res1 and res2 will be true. But when we finally run out, the first getline call will return false (setting res1 to false) and the while condition will thus be false. Therefore the second part of the while condition will not be evaluated and res2 will remain true.

The way to do it is make sure both getline calls are called each time round the loop using a "loop and a half":
Code:
bool res1, res2;
do {
	res1 = getline(read_file1, new_line1); 
	res2 = getline(read_file2, new_line2);

	if ( !res1 || !res2 ) {
		// One of the files has run out. Do whatever needs to be done then break out of the loop

		// Code ...

		break;
	}

	// Both files were read, so do the processing...

} while (res1 && res2);

if (res1 || res2)
{
	// number of lines does not match, as the while loop quit 
	// while one of res1, res2 was true, and the other was false
}