How to compare two files?!

Started by
10 comments, last by Impz0r 22 years, 5 months ago
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
Stay Evil & Ugly!
Advertisement
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.
Dirk =[Scarab]= Gerrits
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
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






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"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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.
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
Stay Evil & Ugly!
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.
hmm,

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

Mfg Impz0r
Stay Evil & Ugly!
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
Stay Evil & Ugly!

This topic is closed to new replies.

Advertisement