C++ Homework debug help needed! Here\'s the homework assignment. _______________
ID: 3886674 • Letter: C
Question
C++ Homework debug help needed!
Here's the homework assignment.
______________________________________________________________________________
Here's my code for the assignment which contains bug
________________________________________________________
here's the output, the output should say the files are equal.
If you can find the bug for me where I did wrong, I did appreciate it and will give you a good rate!
1) Required function: bool cmp files(const std::string& lhsFilename, const std::string& rhsFilename) Before writing functions to modify the files, you've created some test cases by hand and needed a way to automate the tests to determine if your works successfully. To do this, you decide to write a function to tell you if two files are identical, to compare your output files to the tests you created. Write a function cmp_files that takes two filenames as input and returns true if the two files are identical, and returns false otherwise. A suggested way to do this is: CSE 250 Fall 201 Open each file in binary mode. Read the files into memory. Compare if the two files are identical. Return true if the files are equal and false otherwise. · . Note: you should not modify the original file when doing this.Explanation / Answer
The for loop iterates only 1000 times. This is a problem
If the 2 files to be compared were more than 1000 bytes and mismatching byte happens after 1000 bytes, the program output will be incorrect (since it would not match other characters at all)
If the 2 files are less than 1000, for example say 300 bytes. Then the loop goes more than 300 bytes of comparison and the value in the arrays beyond the actually read size will all hold junk values . And comparing the junk values will not match and return false for file comparison. This is the case thats happening for you.. when you are comparing same files (your file size may be less than 1000 and you are going past the file size in the loaded array.)
Also its not necessary to check eof for every iteration since we already know the size of loaded content.
I have given below the corrected implementation with comments to understand whats going on. Hope you find it useful. If it helped, please do rate the answer . Thank you very much.
bool cmp_files(const std::string & lhsFilename, const std::string& rhsFilename){
ifstream lhs, rhs;
lhs.open(lhsFilename, ios::in | ios:: binary | ios::ate);
rhs.open(rhsFilename, ios::in | ios:: binary | ios::ate);
if(!lhs.is_open() || !rhs.is_open()) //check if could not open 1 or both files
return false;
if(lhs.tellg() != rhs.tellg()) //check sizes don't match
return false;
streampos size = lhs.tellg(); //sizes matched, get size from lhs
char *lhsValue;
char *rhsValue;
//get contents of lhs file into allocated array
lhsValue = new char[size];
lhs.seekg(0, ios::beg);
lhs.read(lhsValue, size);
lhs.close();
//get contents of rhs file into allocated array
rhsValue = new char[size];
rhs.seekg(0, ios::beg);
rhs.read(rhsValue, size);
rhs.close();
//now iterate over both the value arrays and compare
bool matched = true;
for(int i = 0; i < size; i++)
{
if(lhsValue[i] != rhsValue[i]) //mismatch, no need for further comparison
{
matched = false;
break;
}
}
//deallocate memory first and return the matching status
delete []lhsValue;
delete []rhsValue;
return matched;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.