Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is C + + program that I am running in with a lot of issues. So it suppoce to be a file comparison program.

This is C++ program that I am running in with a lot of issues.
So it suppoce to be a file comparison program. File 1 has a lot of text that is located on one line only. File 2 has the same text but is on many lines. This program should compare two of these files. If the program runs into a
diffrence it gives the option to skip a certain amount of characters. So the program countinues to compare from the same character in the files (Done because The issues in each file could be diffrent lengths.).
Also it prints the issues to display where the issue is present.
- biggest issue is that when program file 2 runs in to the end of line 1it does not get the text from line 2 to countinue comparing and ends the program.
Can the issue be fixed since I cant get it to run the way I want. Thank you.
#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;
}
Wanted ouput:
./main file1.txt file2.txt
**********************************************************************************************************************************
file1--- Difference at position: 10
>[][Candii___sup","lunchtime3546856///codede","WantToGetzit\,\4536454-3421343768-5676556789-_-smartis
^^^^^^^^^^
fil2--- Difference at position: 10
>[][Candii___sup","lunchtime3546856///codede","WantToGetzit\,\4536454-3421343768-5676556789-_-smarti
^^^^^^^^^^
File 1 shift forward: 1
File 2 shift forward: 2
**********************************************************************************************************************************
file1--- Difference at position: 81
>s3243214123-142/24124___countdowns---Passed\den---sed334??:'Capess'''sometimes::''>>2::"sun"""compar
^^^^^^^^^^
file2--- Difference at position: 82
>q5243214123-142/24124___countdowns---SUN---sed334??:'Capess'''sometimes::''>>2::"sun"""compare/PeepM
^

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

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

More Books

Students also viewed these Databases questions