Skip to content

Commit e7cdade

Browse files
committed
Added better checking functionality
1 parent e9def1f commit e7cdade

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

output_checker.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//Below is used to compare the output of two files
2-
//The "out.txt" file is generated from the "work.cpp" file
3-
//The "res.txt" file is generated from the "check.cpp" file
4-
51
#include <bits/stdc++.h>
62
using namespace std;
73

@@ -14,26 +10,45 @@ int main() {
1410
output2.open("res.txt", ios::in);
1511

1612
if (!output1) {
17-
cout<<"Can't open output file 1\n";
13+
cerr<<"Can't open output file 1\n";
1814
return 0;
1915
}
2016
if (!output2) {
21-
cout<<"Can't open output file 2\n";
17+
cerr<<"Can't open output file 2\n";
2218
return 0;
2319
}
2420

2521
int lines = 1;
2622
string str1, str2;
2723
while (getline(output1, str1)) {
2824
getline(output2, str2);
29-
if (str1 != str2) {
30-
cout<<"Output differs at line number : "<<lines<<"\n";
25+
int a = str1.length();
26+
int b = str2.length();
27+
int lim = min(a, b);
28+
int pos = 0;
29+
bool c = false;
30+
for(int i=0; i<lim; ++i) {
31+
if (str1[i] != str2[i]) {
32+
c = true;
33+
break;
34+
}
35+
pos += 1;
36+
}
37+
if (c || a!=b) {
38+
cerr<<"Output differs at line number : "<<lines<<"\n";
39+
cerr<<"The characters diifer at position : "<<pos+1<<"\n";
40+
if (a < b) {
41+
cerr<<"Output in file out.txt has shorter length\n";
42+
}
43+
else if (b < a) {
44+
cerr<<"Output in file res.txt has shorter length\n";
45+
}
3146
return 0;
3247
}
3348
lines += 1;
3449
}
35-
cout<<"Scanned a total of "<<lines<<" lines from both files\n";
36-
cout<<"Both output files are same\n";
50+
cerr<<"Scanned a total of "<<lines<<" lines from both files\n";
51+
cerr<<"Both output files are same\n";
3752
output1.close();
3853
output2.close();
3954
return 0;

0 commit comments

Comments
 (0)