Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is C + + pgogram that I am running in with a lot of issues. Would you be able to clean up anything unneeded

This is C++ pgogram that I am running in with a lot of issues. Would you be able to clean up anything unneeded or change anything to be more effiecient for the end goal.
So it suppoce to be a file comparison program. File1 has a lot of text that is located on one line only. File2 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.
Issues known in the program.
- There seems to be an issue with the amount of characters procecced giving not an exactly correct amount the longer the text
- biggest issue is that when program file2 runs in to the end of line 1 it 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.
main.cpp:
#include
#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::getline(file1, line1); // Read the entire line from file1
std::vector lines2; // Store lines from file2
while (std::getline(file2, line2)){
// Skip empty lines in file2(ignore line breaks)
if (!line2.empty()){
lines2.push_back(line2);
}
}
// Compare each character in line1 with lines in file2
size_t line2Index =0; // Track the current line number in file2
size_t column2Index =0; // Track the current column number in file2
size_t file1CharCount =0; // Track the total number of characters processed in file1
size_t file2CharCount =0; // Track the total number of characters processed in file2
bool filesIdentical = true; // Flag to track if files are identical
while (line1.length()>0 && line2Index < lines2.size()){
// Check if line2 is empty and skip over it
if (lines2[line2Index].empty()){
++line2Index;
continue; // Skip to the next iteration
}
bool differenceFound = false;
// Compare characters
for (size_t i =0; i < line1.length() && (column2Index + i)< lines2[line2Index].length(); ++i){
++file1CharCount; // Increment character count for file1
++file2CharCount; // Increment character count for file2
if (line1[i]!= lines2[line2Index][column2Index + i]){
std::cout <<"**********************************************************************************************************************************"<< std::endl;
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(column2Index + i,100)<< std::endl;
std::cout <<"^^^^^^^^^^"<< std::endl;
filesIdentical = false;
// Prompt user for number of characters to move forward in each file after the difference
std::cout <<"
"<< "File 1 shift forward: ";
size_t moveForward1;
std::cin >> moveForward1;
file1CharCount += moveForward1; // Add the number of characters moved forward to the total count for file1
std::cout << "File 2 shift forward: ";
size_t moveForward2;
std::cin >> moveForward2;
file2CharCount += moveForward2; // Add the number of characters moved forward to the total count for file2
std::cin.ignore(std::numeric_limits::max(),'
'); // Clear input buffer
// Adjust line1 and line2Index based on user input
line1= line1.substr(i + moveForward1);
column2Index += i + moveForward2;
differenceFound = true;
break; // Exit the loop
}
}
if (!differenceFound){
// Move to the next line in file2 if line1 is shorter
if (line1.length()< line

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

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago