How to compare ./a.out and a .txt file with diff?


So I have a c program, e.g. test1.c, and I wanted to compare it to a text file called test1expectedoutput.txt.

How would I do this?

I compiled the c file (written in nano) by doing gcc test1.c, but how do I compare it?

Comparing the .c file to the .txt obviously does not work as it just prints both of them out in full randomly, but if I try compare in that file directory with diff using:

diff ./a.out test1expectedoutput.txt

To which I get the message:

Binary files ./a.out and test1expectedoutput.txt differ

So I’m not 100% sure how to compare them and know what the differences are? To put it simply, say the program in test1.c just says:


and the test1expectedoutput.txt just reads (with cat):


How could I compare these after compiling?


Both Sanat and Mythos are correct.

What I ended up doing was doing the ./a.out > output.txt then using diff, the diff (if the file is correct) doesn’t output something so what I like to use is diff -s text1.txt text2.txt which will then, if identical to double check, output that they are identical.

The problem is, it was not identical and it wouldn’t tell me why, no matter what I tried and colours, so I manually nano’ed both text files, and went in, and just after messing around found that one randomly had an extra space, normally it would colour the differences but it doesn’t colour spaces… so that was fun.

Answered By – CantCodeForShit

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published