comparing files

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
lightspdAsked:
Who is Participating?
 
CetusMODConnect With a Mentor Commented:
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0
 
jkrCommented:
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
 
lightspdAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jkrCommented:
With the output from above, you can format that any way you want...
0
 
lightspdAuthor Commented:
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
 
dbkrugerCommented:
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
 
lightspdAuthor Commented:
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
 
jkrCommented:
I'd object since the above code does what was asked for.
0
 
VenabiliCommented:
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
 
jkrCommented:
I am not sure whether a formatting issue qualifies that as unanswered, that's more a followup request.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.