Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can you help with building a C + + program for file comparison. That can be efficient and work with large files. requirements: 1 .
Can you help with building a C program for file comparison. That can be efficient and work with large files.
requirements:
the program should open two files that can ignore line breaks
the program should run through the files and identify difrences or if same
the program should print out the line and column for location purposes
the program should give error if one file is longer or shorter than the other file
the program should countinue running to countinue looking for diffrences if it encounters one
the program should print where the issue has appeared in the text
the program can countinue checking after the diffrence line of characters is presented unless there is a better way
Example file Is much more complex:
abcabcabcabcabcabcabcabcabcabcabcabc
Example file Is much more complex:
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
Wanted output of program or something close to this:
if same:
filetxt and filetxt are identical.
if diffrent:
filetxt and filetxt are diffrent.
Occurance :
filetxt has an occurance at line: # column: #
Diffrence present in:
filetxt has an occurance at line: # column: #
Diffrence present in:
if diffrent:
filetxt and filetxt are diffrent.
Occurance :
filetxt has an occurance at line: # column: #
Diffrence present in:
filetxt has an occurance at line: # column: #
Diffrence present in:
etc.
I received this code from an expert but it does not work. and not sure how I would even implement it
#include
#include
#include
void compareFilesconst std::string& file const std::string& file
std::ifstream streamfile;
std::ifstream streamfile;
if streamstream
std::cerr "Error opening files." std::endl;
return;
std::string line line;
int lineNum ;
while std::getlinestream line && std::getlinestream line
lineNum;
Ignore line breaks and compare line content
if line line
std::cout "Files are different. Occurrence lineNum :
;
std::cout file has an occurrence at line: lineNum column:
;
std::cout "Difference present in: line
;
std::cout file has an occurrence at line: lineNum column:
;
std::cout "Difference present in: line
;
Check if one file is longer than the other
if streameof && streameof
std::cout "Files are different. file is shorter than file
;
else if streameof && streameof
std::cout "Files are different. file is longer than file
;
else
std::cout "Files are identical.
;
int main
std::string file "filetxt;
std::string file "filetxt;
compareFilesfile file;
return ;
running it with a small files that should be identical it gives an error where it should not as you can see in the output below:
zip
Files are different. Occurrence :
Textmatchtxt has an occurrence at line: column:
Difference present in: abcabcabc
Textmatchtxt has an occurrence at line: column:
Difference present in: abc
And running with a large files it just runs non stop and just prints the files nonstop.
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