2

I wrote a python 3 script which checks if 2 output text files of C code are identical. 1 file is the "wanted" output text file which i got as an example of an input/output file with the assignment, and the other is the output file which my C code has given as output.

import filecmp

f1="out1.txt" #the output file i got from the assignment as example
f2="myout1.txt" #the output file my c code gave for the same input file of f1
f2_copy="myout1 (copy).txt" #copy of f2
print(filecmp.cmp(f1,f2))
print(filecmp.cmp(f2,f2_copy))

and i get as output:

>>>False 
>>>True

even-though the files are identical, I get False as an output. The only way i get True is when i copy the file and then do filecmp.cmp...

if it matters, I'm using Ubunto and GCC to get the output file...

thanks.

EDIT1: I made a function which reads the lines of each file and compare the 2 lists:

def textCompare(fl1,fl2):
    file1 = open(fl1, 'r')
    file2 = open(fl2, 'r')
    lines1=file1.readlines()
    lines2=file2.readlines()
    f1.close()
    f2.close()
    if lines1 == lines2:
        return True 
    else:
        return False

as I see, for my purposes it works fine... my question is what is the diffrence between this function and filecmp.cmp() that my function doesnt check?

Yoni Newman
  • 175
  • 13
  • 1
    I'd guess you have a whitespace difference then, e.g. different line endings, or a missing line break on the last line. Do the files have the same size on disk? Can you try a local diff tool e.g. diff? – Rup Mar 22 '18 at 16:02
  • 1
    Comparing files using Python is not really a C language issue. – Gerhardh Mar 22 '18 at 16:21
  • Thanks for the quick responding. as I said the 2 files are identical (i.e same size and content). When I try to use diff f1 f2 it gives me the same result as filecmp.cmp(f1,f2) and works fine with the copy of the file... – Yoni Newman Mar 22 '18 at 16:44
  • You've shown us that copy & pasting them into a website says they're the same, but there may be subtle differences that the copy & paste process eliminates, or that that website ignores. If you're saying that diff says they're different then it should show you why it thinks they're different. – Rup Mar 22 '18 at 16:53
  • when I use diff I get the exit code of "1,31c1,31" (I have 31 lines in the texts). but it doesnt tell me where are the diffrence. When I change the first raw for 1 of the files I still get the same exit code of 1,31c1,31. – Yoni Newman Mar 22 '18 at 17:03
  • And it prints your whole file twice, once with < and once with >? That is typical of line ending mismatch. But then I'd expect one file to be 31 bytes bigger than the other. Can you try hexdump-ing the files and compare the hexdumps? – Rup Mar 22 '18 at 17:09
  • Hey Rup, I just edited my post. can u check the question I asked? – Yoni Newman Mar 22 '18 at 17:35
  • Another tool you can try is `cmp`. – Arndt Jonasson Mar 23 '18 at 13:26

0 Answers0