Question: #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;
}
 #include #include #include #include bool compareFiles(const std::string& filePath1, const std::string& filePath2){

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!