Solved

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

Posted on 2003-10-21
6
211 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
ID: 9590975
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
ID: 9591151
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
ID: 9591180
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 8

Expert Comment

by:Exceter
ID: 9591217
>> 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
ID: 9591247
EarthQuaker, it seams we posted similar code. You posted while I was still typing. :-)
0
 
LVL 3

Expert Comment

by:EarthQuaker
ID: 9595670
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

828 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