Solved

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

Posted on 2003-10-21
6
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 82
c++ syntax question 9 63
White board coding practice 3 98
Winapi.Windows.hpp problem 7 35
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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