• C

Differences between two sets of texts

Hi, If i have 2 sets of texts (stored in an array, not that that should matter)
and i need to specify all the differences on one of them, is there a function that automatically compares an entire text returning all the positions (perhaps) where the texts begin to differ and end differing.  
More specifically: I have a program that grabs html from a site, and then grabs it again a few seconds later to check whether it has changed.  If the texts differ, then the more recent text has to be printed to stdout, highlighting (changing the font to red) where the more recent text is different.
If there isn't already a function, what would be a good method to go about this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can just pipe them through diff and capture the output and perhaps status code.  For example

   rc = system("diff oldversion newversion > diffs.txt");
   if (rc) {
      /* An error occurred */
      fprintf(stderr, "The files are different. \n");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
if you are on *nix machine, then you can use diff command...
if you want to implement it on windows, you can look into the source of diff ... anyway, the source for such function should be easy

suppose your old text is in char array old[] and new text is in char array new[]

char *p, *q;

p = old;
q = old;

while ( p != NULL && q != NULL )
        if ( *p != *q )
                 /* this char is different in both ... handle it*/

if ( p == NULL && q != NULL )
    /* q (new) has some excessive text handle it*/

if ( q == NULL && p != NULL )
    /* p (old) has some excessive text handle it*/
sunnycoder, I think your algorithm won't serve the goal Schdeffon wants it for.

For example, assume there's some counter in the body of the HTML, which changes from time to time.

If you compare 2 pages, where the counter is 9 and 10 - the entire file (starting from that point) will be different, as all the HTML text is shifted in the second case. Bu logically you wanted to find that 9 became 10.

I think that more complex algorithms should be used, which find the minimal differences between the files (as I believe diff will do).
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

that was what the asker seemed to be asking
"grabs it again a few seconds later to check whether it has changed.  If the texts differ, then the more recent text has to be printed to stdout, highlighting (changing the font to red) where the more recent text is different."

In case diff kind of out put is reqd., I guess this should do

int i = 0;
oldf = fopen(...);
newf = fopen(...);

while ( fgets(oldbuffer...) != NULL and fgets(newbuffer... != NULL )
         if ( strcmp (oldbuffer, newbuffer)  != 0 )
               printf ( "line no. %d is differernt\n", i );

a comparison routine can be added in place of printf to get the char/column number where text differs
EOF tests can be performed after the while loop to test if the number of lines are different in two cases
A decent line by line diff is a lot more work.  It's not simply a matter of identifying the differences, but you also need to find the places where things match up again.  In the case of HTML, it's also useful to ignore differences in whitespace.  

That's why I recommended just using the system diff, getting a free one (e.g. GNU diff) for MS Windows if necessary.  
There's no point in reinventing the wheel.

Kent OlsenDBACommented:

Hi Schdeffon,

A couple of pieces of good advice have been posted, but it gets even more compicated than that.  Changing a single value in an html document can result in line breaks changing which will change the number of lines and may change how the html is endented or othwise formatted.  This can strongly complicate the amount of work needed to test for the differences.

If this were my task, I'd read the two files into separate buffers and walk through them byte by byte.  At each byte I would then make certain value judgements.

If the bytes are the same, loop to the next byte.
If the bytes are both whitespace, skip to the next non-whitespace in each line.
If the bytes are both characters or digits, check to see if the WORD that starts at the current locations match.  (If both characters being pointed to are "A", assemble a word from each file and see if the words are different.  If they are different, you don't really care that a 1 in the first file became a 2 in the second as much as you'll care that the word "Address1" became "Address2" or that the value 101 became the value 102.)  You may want to disregard case.
If the bytes are both quote marks, build the corresponding strings and test them.
Finally, test the two bytes.  If they are not the same, mark a difference.

Resyncing after finding a difference can be tough.  That's a science unto itself.

Good Luck,
One approach listed is what I've used in the past...it's tough but possible.

Take an html page, break it into a parse tree where all elements are branches, and all values are leaves. Then write a tree compare function, if you need case sensitive or case insensitive it may be better to add a flag for that.

You get the whitespace, indentation, newline issues solved, but the complexity does go up.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.