[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

comparing files

Posted on 2006-05-23
12
Medium Priority
?
237 Views
Last Modified: 2010-04-01
I want to compare two text files.  I'm trying to think of a way to compare the lines and if the line is different bold it.  The problem is the line order can change.  I already  have the input/output and everything else done, the compare part is the only thing that is holding me up.  here is a example of how I see it.

file1.  file2
1-a      2-b
2-b      1-c
3-c      3-c
          4-d

the output would be
1-a  2-b
2-b  <B>1-c</B>
3-c  3-c
     <B>4-d</B>

I was originally just going to put the two files next to eachother, till someone pointed out why not just have the program compare the files.  

I hope that makes since.

Thanks
0
Comment
Question by:lightspd
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 16745371
Are you sure that this is the output you are expecting? As far as I understand, you want to output the lines that are *not* in both file1 *and* file2. Then, the output should be

1-a
1-c
4-d

Or in code:

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

using namespace std;

void read_file(char* name, set<string>&s) {

    ifstream is(name);

    while (!is.eof()) {

        string str;

        getline(is,str);

        s.insert(str);

    }

}

int main() {


    set<string> s1;
    set<string> s2;

    read_file("file1.txt",s1);
    read_file("file2.txt",s2);

    set_symmetric_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),ostream_iterator<string>(cout,"\n"));

    return 0;
}
0
 

Author Comment

by:lightspd
ID: 16745634
close....I do want the output to to put both files and then the differences hightlighted, for example.  I was thinking that I would make the output file a html, so I could just add a bold statement at the front of the line that is different.  I was thinking of something along the lines of.....compare line 1 of file 2 to all lines in file 1 and if there is no different combine both line 1's of the files and output to new file or if there is a difference combine both lines but add a <b> in front of the line 1 from file2....Does that make since?

Cheers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 16745816
With the output from above, you can format that any way you want...
0
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!

 

Author Comment

by:lightspd
ID: 16746488
Maybe I'm missing something....from what I know the set_symmetric function just returns the differences between files.  What I want to try to do is output both files to 1 file, but have the differnces hightlights.  So the question is how can I have it so the output will be more like this. or so.

file1.
1,line1
2,line2
3,line3

file2
1,line1a
3,line3b
2,line2
4,line4

now with that function my output would be
1,line1a
3,line3b
4,line4

what I would like is something like this.

1,line1    1,line1a <- highlighed or something
2,line2    3,line3b <-
3,line3    2,line2
              4,line4 <-

Thanks
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 16756139
Your original question was about algorithms. Presumably you want to do this fast?
The diff utility in Unix has for a long time had a very slick algorithm for recognizing sequences of text.

Here's some code in perl: http://search.cpan.org/~nedkonz/Algorithm-Diff-1.15/lib/Algorithm/Diff.pm

Wikipedia gives you a sense of the history and the algorithm: http://en.wikipedia.org/wiki/Diff

Consider using diff itself as the engine, and just format the output, rather than trying to write your own from scratch.
0
 

Author Comment

by:lightspd
ID: 16764135
actually I was thinking....avoiding algorithms, it would be faster and more efficient, but is there and easy way just to use some while loops.  for this example file2 is always going to have more lines than file1.  So you grab line1 from file2 and compares it to all lines in file1, if finds a match just output line1 from both file1 & 2, if it doesn't find a match combine lines1 from file1&2 but bold line1 from file2.  

let me know if something in there doesn't make since.  yes I know it's slow and not very efficient but it will work for what I need it for.

Cheers.
0
 
LVL 86

Expert Comment

by:jkr
ID: 17046328
I'd object since the above code does what was asked for.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 17046719
The Asker decided to take another route.
I wondered if it is a PAQ or an award. Noone answered to the ping for 2 weeks so obviously noone felt they answered it... :)
I agree that the code DOES work. However - the Asker decided something else...
0
 
LVL 86

Expert Comment

by:jkr
ID: 17047605
I am not sure whether a formatting issue qualifies that as unanswered, that's more a followup request.
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 17068471
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

873 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