Link to home
Start Free TrialLog in
Avatar of hemanexp
hemanexp

asked on

Comparing two text files...........

Hi,

  I have two text files. Each file contains huge data, say 1000 to 2000 lines. Now i want to compare these two files and produce the differences.
 
  Since the file size is big, i dont want to perform character by character comparision. One point that we have to consider is, each line in the file doesnt exceed 512 characters. This helps us to comapare line by line comaprision.

  I was wondering if anybody has an idea of how i could do this.

 
Thanx.
   
Avatar of jkr
jkr
Flag of Germany image

Why reinventing the wheel? Check out the GNU 'diff' code: http://www.gnu.org/software/diffutils/diffutils.html
Avatar of Member_2_1001466
Member_2_1001466

Or have a look at http://winmerge.sourceforge.net/ to get the sources for winmerge.
ASKER CERTIFIED SOLUTION
Avatar of EarthQuaker
EarthQuaker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> and produce the differences.

What do you mean by this?

You could always say something like,

ifstream in1( "myfile1.txt", ios::in ), in2( "myfile2.txt", ios::in );
string line1, line2, relationship;
unsigned int counter = 0;
int result;

while( in1 && in2 )
{
    in1 >> line1;
    in2 >> line2;
    result = line1.compare( line2 );

    if( result == 0 )
        relationship = "equal to";
    if( result < 0 )
        relationship = "less than";
    if( result > 0 )
        relationship = "greater than";

    cout << "Line " << counter << " in file 1 is " << relationship << " line " << counter << " in file 2."  << endl;

    ++counter;
}

in1.close();
in2.close();

It would be helpfull if you clarified what you meant by the "differences" between the files.

Note: I did not compile this.

Cheers!
Exceter
EarthQuaker, it seams we posted similar code. You posted while I was still typing. :-)
Exceter, no worries.

But I'll maybe just notice that your code processes the file word per word without comparing spaces, newlines char and others skipped chars by operator>>()

Nothing really bad, but as you said the user should refine what he means by "difference".