Solved

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

Posted on 2003-10-21
6
208 Views
Last Modified: 2010-04-02
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.
   
0
Comment
Question by:hemanexp
6 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Why reinventing the wheel? Check out the GNU 'diff' code: http://www.gnu.org/software/diffutils/diffutils.html
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Or have a look at http://winmerge.sourceforge.net/ to get the sources for winmerge.
0
 
LVL 3

Accepted Solution

by:
EarthQuaker earned 100 total points
Comment Utility
I'd use jkr's solution.

However, comparing differences line per line is easy to write :

#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file1("foo.dat", ios::binary);
    ifstream file2("bar.dat", ios::binary);

    if(!file1 || !file2)
        return 1;

    int line=0;
    string s1,s2;
    while(getline(file1,s1) && getline(file2,s2))
    {
        line++;
        if(s1!=s2)
        {
            cout << "Difference at line " << line << " :" << endl;
            cout << "File1 : " << s1 << endl;
            cout << "File2 : " << s2 << endl;
            cout << "-----------------" << endl;
        }
    }    

    // TODO : handle different sizes of files

    return 0;
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:Exceter
Comment Utility
>> 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
0
 
LVL 8

Expert Comment

by:Exceter
Comment Utility
EarthQuaker, it seams we posted similar code. You posted while I was still typing. :-)
0
 
LVL 3

Expert Comment

by:EarthQuaker
Comment Utility
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".
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now