Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to diff files from C++?

Tags:

c++

c

diff

patch

I'm looking for a C or C++ diff library. I know I can use the Unix diff tool in combination with system or exec, but I really want a library. It would be handy if the library could perform patches as well, like the Unix patch tool.

like image 929
Matt Fichman Avatar asked Sep 20 '09 18:09

Matt Fichman


People also ask

How can I compare two C files?

Step 1: Open both the file with pointer at the starting. Step 2: Fetch data from file as characters one by one. Step 3: Compare the characters. If the characters are different then return the line and position of the error character.

What command is used to compare the files?

Explanation: diff command is used for comparing files and displaying the differences between them.


3 Answers

I think I've found a good solution, finally:

The DTL - Diff Template Library --- Tutorial

It supports patch. I had to type "diff.cpp" into Google to find it. Hopefully it works!

like image 111
Matt Fichman Avatar answered Sep 22 '22 11:09

Matt Fichman


It seems like the Google Diff, Match and Patch libraries are what you need.

like image 43
Paul Biggar Avatar answered Sep 23 '22 11:09

Paul Biggar


This is an implementation of a "solution to SES/LCS with the Hirschberg linear space refinement as described in the following publication":

E. Myers, ``An O(ND) Difference Algorithm and Its Variations,'' Algorithmica 1, 2 (1986), 251-266. http://www.cs.arizona.edu/people/gene/PAPERS/diff.ps

Found it on the Wikipedia page on diff.

That's only for finding a diff though, not applying it as a patch. I think that application of a patch is actually a harder problem; due to the risk of conflicts. It would need some form of user-controlling feedback mechanism, to resolve conflicts.

like image 39
unwind Avatar answered Sep 25 '22 11:09

unwind