Differences between two sets of texts

Posted on 2003-11-09
Last Modified: 2010-04-15
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?
Question by:Schdeffon

Accepted Solution

GaryFx earned 63 total points
ID: 9709640
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");

LVL 45

Expert Comment

ID: 9709645
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*/

Expert Comment

ID: 9709946
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).
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 45

Expert Comment

ID: 9709960
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

Expert Comment

ID: 9710689
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.

LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 62 total points
ID: 9712052

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,

Expert Comment

ID: 9714947
One approach listed is what I've used in the'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.

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Read file descriptor 3 in C 14 113
c++ substatte a varabe for a string in a LPCTSTR statment 8 89
IIS Log files on Exchange 2013 server 6 180
Why is compiler in oracle server ? 9 75
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question