问题
So I will start by saying this is for a course and I assume the professor won't really care that they are the same if cmp returns something weird. I am attempting to compare the output of my code, named uout, to the correct output, in the file correct0. The problem however is that it returns "cmp: EOF on uout". From a little bit of digging I found that EOF indicates they are the same up to the end of the shorter file with the shorter file being the one named after EOF, so what I gather from this is that they are the same until uout ends short. Problem is however, that it absolutely does NOT end short. When opening both in a text editor and manually checking spaces, line and column numbers, etc. everything was an EXACT match.
To illustrate my point here are the files copied directly using ctrl-a + ctrl-v:
correct0 http://pastebin.com/Bx7SM7rA
uout http://pastebin.com/epMFtFpM
If anyone knows what is going wrong and can explain it simply I would appreciate it. I have checked multiple times and can't find anything wrong with it. Maybe it is something simple and I just can't see it, but everything I have seen so far seems to suggest that the files are the same up until the "shorter one" ends, and oddly even if i switch my execution from
cmp correct0 uout
to
cmp uout correct0
both instances end up returning
cmp: EOF on uout
回答1:
The files you uploaded are same. It can be a line ending problem. DOS/Windows uses "\r\n" as a line ending, but Unix/Linux uses just a "\n".
The best utility on Linux machine for checking what your problem is, is "od" (octal dump) or any other command for showing files in their binary format. That is:
$ od -c uout.txt
0000000 E n t e r t h e n u m b e r
0000020 s f r o m 1 t o 1 6 i
0000040 n a n y o r d e r , s e p
0000060 a r a t e d b y s p a c e s
0000100 : \r \n \r \n 1 6 3 2 1
0000120 3 \r \n 5 1 0 1 1 8 \r
0000140 \n 9 6 7 1 2 \r \n
0000160 4 1 5 1 4 1 \r \n \r \n R
0000200 o w s u m s : 3 4 3 4 3
0000220 4 3 4 \r \n C o l u m n s u m
0000240 s : 3 4 3 4 3 4 3 4 \r \n
0000260 D i a g o n a l s u m s : 3
0000300 4 3 4 \r \n \r \n T h e m a t r
0000320 i x i s a m a g i c s q
0000340 u a r e
0000344
As you can see, here the line endings are \r\n. Since you have opened and copy pasted the files, this represents your machines preferences and not the actual fiels line ending. Also you can try dos2unix utility to convert line endings.
回答2:
If the files are human readable I would use diff
tool instead. It has ways to ignore the line endings(see the --ignore-space-change
and --strip-trailing-cr
and --ignore-blank-lines
).
diff -u --ignore-space-change --strip-trailing-cr --ignore-blank-lines test_cases/correct0 test_cases/uout0
来源:https://stackoverflow.com/questions/28950382/cmp-command-returning-eof-on-my-output-despite-exact-match-as-far-as-i-can-tell