File tree 1 file changed +25
-10
lines changed
1 file changed +25
-10
lines changed Original file line number Diff line number Diff line change 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
-
5
1
#include < bits/stdc++.h>
6
2
using namespace std ;
7
3
@@ -14,26 +10,45 @@ int main() {
14
10
output2.open (" res.txt" , ios::in);
15
11
16
12
if (!output1) {
17
- cout <<" Can't open output file 1\n " ;
13
+ cerr <<" Can't open output file 1\n " ;
18
14
return 0 ;
19
15
}
20
16
if (!output2) {
21
- cout <<" Can't open output file 2\n " ;
17
+ cerr <<" Can't open output file 2\n " ;
22
18
return 0 ;
23
19
}
24
20
25
21
int lines = 1 ;
26
22
string str1, str2;
27
23
while (getline (output1, str1)) {
28
24
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
+ }
31
46
return 0 ;
32
47
}
33
48
lines += 1 ;
34
49
}
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 " ;
37
52
output1.close ();
38
53
output2.close ();
39
54
return 0 ;
You can’t perform that action at this time.
0 commit comments