Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include bool compareFiles ( const std::string& filePath 1 , const std::string& filePath 2 ) { std::ifstream file 1 ( filePath 1 )

#include
#include
#include
#include
bool compareFiles(const std::string& filePath1, const std::string& filePath2){
std::ifstream file1(filePath1), file2(filePath2);
if (!file1||!file2){
std::cerr "Error: Unable to open files." std::endl;
return false;
}
std::string line1, line2;
std::vector lines2;
while (std::getline(file2, line2)){
if (!line2.empty()){
lines2.push_back(line2);
}
}
size_t line2Index =0;
size_t file1CharCount =0;
size_t file2CharCount =0;
bool filesIdentical = true;
while (std::getline(file1, line1)){
if (line1.empty()|| line2Index >= lines2.size()){
break;
}
bool differenceFound = false;
for (size_t i =0; i line1.length() && (file2CharCount + i) lines2[line2Index].length(); ++i){
++file1CharCount;
++file2CharCount;
if (line1[i]!= lines2[line2Index][file2CharCount -1]){
std::cout filePath1"- Difference at position: " file1CharCount std::endl;
std::cout ">" line1.substr(i,100) std::endl;
std::cout "^^^^^^^^^^" std::endl;
std::cout filePath2"- Difference at position: " file2CharCount std::endl;
std::cout ">" lines2[line2Index].substr(file2CharCount -1,100) std::endl;
std::cout "^^^^^^^^^^" std::endl;
filesIdentical = false;
std::cout "File 1 shift forward: ";
size_t moveForward1;
std::cin >> moveForward1;
file1CharCount += moveForward1;
std::cout "File 2 shift forward: ";
size_t moveForward2;
std::cin >> moveForward2;
file2CharCount += moveForward2;
std::cin.ignore(std::numeric_limits::max(),'
');
line1= line1.substr(i + moveForward1);
differenceFound = true;
break;
}
}
if (!differenceFound){
++line2Index;
file2CharCount =0;
}
}
return filesIdentical;
}
int main(){
std::string file1, file2;
std::cout "Enter path to File1: ";
std::cin >> file1;
std::cout "Enter path to File2: ";
std::cin >> file2;
if (compareFiles(file1, file2)){
std::cout "Files are identical." std::endl;
} else {
std::cout "Files are different." std::endl;
}
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions