Answered step by step
Verified Expert Solution
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. File has a lot of text that is located on one line only. File 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 file runs in to the end of line it does not get the text from line 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 compareFilesconst std::string& filePath const std::string& filePath
std::ifstream filefilePath filefilePath;
if filefile
std::cerr "Error: Unable to open files." std::endl;
return false;
std::string line line;
std::getlinefile line; Read the entire line from file
std::vector lines; Store lines from file
while std::getlinefile line
Skip empty lines in fileignore line breaks
if lineempty
linespushbackline;
Compare each character in line with lines in file
sizet lineIndex ; Track the current line number in file
sizet columnIndex ; Track the current column number in file
sizet fileCharCount ; Track the total number of characters processed in file
sizet fileCharCount ; Track the total number of characters processed in file
bool filesIdentical true; Flag to track if files are identical
while linelength && lineIndex linessize
Check if line is empty and skip over it
if lineslineIndexempty
lineIndex;
continue; Skip to the next iteration
bool differenceFound false;
Compare characters
for sizet i ; i linelength && columnIndex i lineslineIndexlength; i
fileCharCount; Increment character count for file
fileCharCount; Increment character count for file
if linei lineslineIndexcolumnIndex i
std::cout std::endl;
std::cout
filePath Difference at position: fileCharCount std::endl;
std::cout linesubstri std::endl;
std::cout std::endl;
std::cout
filePath Difference at position: fileCharCount std::endl;
std::cout lineslineIndexsubstrcolumnIndex i 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 shift forward: ;
sizet moveForward;
std::cin moveForward;
fileCharCount moveForward; Add the number of characters moved forward to the total count for file
std::cout "File shift forward: ;
sizet moveForward;
std::cin moveForward;
fileCharCount moveForward; Add the number of characters moved forward to the total count for file
std::cin.ignorestd::numericlimits::max
; Clear input buffer
Adjust line and lineIndex based on user input
line linesubstri moveForward;
columnIndex i moveForward;
differenceFound true;
break; Exit the loop
if differenceFound
Move to the next line in file if line is shorter
if linelength line
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started