#### Archived

This topic is now archived and is closed to further replies.

# How to compare two files?!

This topic is 5963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m searching for an algorithm to compare two files, to find out which data has deleted, added, overwrote. If some one has an code snipped or an linkt to one, tips are welcome *g* Thanks alot... Mfg Impz0r

##### Share on other sites
I don''t have some code for you unfortunately. I do know however that my favorite text/hex editor has an excellent compare files function.

##### Share on other sites
It depends on what you want to compare.
If you want to compare a char by char value in the file or compare the fields in the file.
For a field compare
Open both files
Parse both of the files and store in an object
Compare the objects.

This is not the fast way but it depends on how deep you want the compare to go. Need more info if you want a better answer

##### Share on other sites
The pseudocode would be like:

  1) Open both files, get a handle to them2) While (true) i) Read a byte from each file ii) If the bytes are equal, continue iii) Otherwise, break the loop

MatrixCubed
http://MatrixCubed.org

##### Share on other sites
Can''t you track down the source to the GNU diff program, which does exactly what you want(Or you could just use diff)? This is a good place to start searching.

"A society without religion is like a crazed psychopath without a loaded .45"

##### Share on other sites
Actually, Dean Harding (he used to hang around here) found that there was no modular diff library on the net, so he wrote one. Look for it on his site.

##### Share on other sites
Impressiv,

So many replays in this short time thanks a lot guys :D
Ok my main reason why i would compare two files is, i would make a patcher, i think you know what that is :D
And for this i have to find out which data has changed and find out if they deleted. If so, i havn''t store these data in the patch, else i store the changed data in the patch file. And there i run into problems, the main function to compare two files did i have. But i don''t know how to find out which data has deleted. Sory that i didn''t post these in my first post :D

Hope xou can help me out there :D

Mfg Impz0r

##### Share on other sites
How do patches work? Check it out.

MatrixCubed gave you the pseudocode algorithm for diff''ing two binary files (comparing by bytes). You''ll need to take care to ensure you recognize the difference between insertions (a new section of information) and modifications (overwriting the old info) - and both may occur in a file multiple times.

The GNU link Arild Fines gave you is also a good one; GNU has diff and patch utils (GNU patch works on binaries and text files) with freely available source code.

Finally, check these search results for information on diff and patch.

##### Share on other sites
hmm,

Ok i would check out these links, thanks for your fast help :D

Mfg Impz0r

##### Share on other sites
y00,

Damn I''m back *g*, ok i got the GNU things, but i think these are a litle bit to complex for me. Hmmm, did some body know other Code snippets that can help me ?!

Thanks alot

Mfg Impz0r

##### Share on other sites
Study them; patching arbitrary file types isn''t easy. In fact, nothing good comes easy.

##### Share on other sites
hmm,

Your right, thats true, hmmm but i think it must exists some other free patch code, if some here did know where i can find it pleas let me know :D

Mfg Impz0r