Question: Can you help with building a C + + program for file comparison. That can be efficient and work with large files. requirements: 1 .

Can you help with building a C++ program for file comparison. That can be efficient and work with large files.
requirements:
1. the program should open two files that can ignore line breaks
2. the program should run through the files and identify difrences or if same
3. the program should print out the line and column for location purposes
4. the program should give error if one file is longer or shorter than the other file
5. the program should countinue running to countinue looking for diffrences if it encounters one
6. the program should print where the issue has appeared in the text
7. the program can countinue checking after the diffrence line of characters is presented unless there is a better way`
Example file 1(Is much more complex):
abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123
Example file 2(Is much more complex):
abc123
abc123
abc123
abc123
abc123
abc123
abc123
abc123
abc123
abc123
abc123
abc123
Wanted output of program or something close to this:
if same:
file1.txt and file2.txt are identical.
-------------------------------------------------------
if diffrent:
file1.txt and file2.txt are diffrent.
Occurance 1:
file1.txt has an occurance at line: # column: #
Diffrence present in:
****************************************
^
file2.txt has an occurance at line: # column: #
Diffrence present in:
****************************************
^
-------------------------------------------------------
if diffrent:
file1.txt and file2.txt are diffrent.
Occurance 2:
file1.txt has an occurance at line: # column: #
Diffrence present in:
****************************************
^
file2.txt has an occurance at line: # column: #
Diffrence present in:
****************************************
^
-------------------------------------------------------
etc.
I received this code from an expert but it does not work. and not sure how to fix it.
#include
#include
void compareFiles(const std::string& file1, const std::string& file2){
std::ifstream stream1(file1);
std::ifstream stream2(file2);
if (!stream1||!stream2){
std::cerr "Error opening files." std::endl;
return;
}
std::string line1, line2;
int lineNum =0;
while (std::getline(stream1, line1) && std::getline(stream2, line2)){
lineNum++;
// Ignore line breaks and compare line content
size_t pos =0;
while ((pos = line1.find_first_not_of("\t\r
", pos))!= std::string::npos){
// Check if characters are different
if (line1[pos]!= line2[pos]){
std::cout "Files are different. Occurrence " lineNum ": " std::endl;
std::cout file1" has an occurrence at line: " lineNum " column: " pos +1 std::endl;
std::cout "Difference present in:" std::endl;
std::cout line1 std::endl;
std::cout std::string(pos,'')"^" std::endl;
std::cout file2" has an occurrence at line: " lineNum " column: " pos +1 std::endl;
std::cout "Difference present in:" std::endl;
std::cout line2 std::endl;
std::cout std::string(pos,'')"^" std::endl;
break;
}
pos++;
}
}
// Check if one file is longer than the other
if (stream1.eof() && !stream2.eof()){
std::cout "Files are different. " file1" is shorter than " file2 std::endl;
} else if (!stream1.eof() && stream2.eof()){
std::cout "Files are different. " file1" is longer than " file2 std::endl;
} else {
std::cout "Files are identical." std::endl;
}
}
int main(){
std::string file1= "Text/largeFile.txt";
std::string file2= "Text/largeFile2.txt";
compareFiles(file1, file2);
return 0;
}
running it with a small files that should be identical it gives an error where it should not as you can see in the output below per the program not ignoring line breaks:
*
./zip
Files are different. Occurrence 1:
Text/match1.txt has an occurrence at line: 1 column: 7
Difference present in:
abc123abc123abc123
^
Text/match2.txt has an occurrence at line: 1 column: 7
Difference present in:
abc123
^
Files are different. Text/match1.txt is shorter than Text/match2.txt
*
And running with a large files it just runs non stop and just prints the files nonstop.
 Can you help with building a C++ program for file comparison.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!