Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-10-21
6
Medium Priority
?
229 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 300 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

885 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