Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please work on C++ Required function: bool cmp_files(const std::string& lhsFilen

ID: 3889562 • Letter: P

Question

Please work on C++

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:

- 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.
Assumptions you can make for cmp_files(…):
- The files referred to by lhsFilename and rhsFilename exist.
- The file referred to by filename is not empty.
- The number of bytes in each of the files is divisible by 4.
- You have read permission on the directory

working code:

#include <fstream>
#include <string>
#include <algorithm>

using namespace std;
bool cmp_files(const std::string& lhsFilename, const std::string& rhsFilename) {

//TODO
}

test code(You cannt change anything here)

#include <iostream>

int main() {
std::string testFilename;

// Tests your cmp_files function on test01 through test09
for (int i = 1; i <= 9; ++i) {
testFilename = std::string("test0") + (char) (i + 48);
if (cmp_files(testFilename, testFilename)) {
std::cout << "cmp_files correctly checks that " << testFilename << " is equal to itself!" << std::endl;
} else {
std::cout << "Something went wrong comparing the file with itself. :(" << std::endl;
return -1;
}
}

Explanation / Answer

I would have written something like this...

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// This function returns all the bytes from file and returns the vector.
// This is an optimized way of fetching file content.
static std::vector<char> ReadAllBytsFromFile(const std::string& file)
{
ifstream filestream(file, ios::binary|ios::ate);
ifstream::pos_type pos = filestream.tellg();

std::vector<char> filecontent(pos);

filestream.seekg(0, ios::beg);
filestream.read(&filecontent[0], pos);

return filecontent;
}

bool cmp_files(const std::string& lhsFilename, const std::string& rhsFilename) {
  
std::vector<char> lhsfilecontent, rhsfilecontent;
  
lhsfilecontent = ReadAllBytsFromFile(lhsFilename);
rhsfilecontent = ReadAllBytsFromFile(rhsFilename);
  
int size = lhsfilecontent.size();
if (size != rhsfilecontent.size()) {
return false;
}
  
int i;
bool compOut = true;
for (i = 0; i < size; i++){
if(lhsfilecontent[i] != rhsfilecontent[i]) {
compOut = false;
break;
}
}
return compOut;
}

int main() {
std::string testFilename;
// Tests your cmp_files function on test01 through test09
for (int i = 1; i <= 9; ++i) {
testFilename = std::string("test0") + (char) (i + 48);
if (cmp_files(testFilename, testFilename)) {
std::cout << "cmp_files correctly checks that " << testFilename << " is equal to itself!" << std::endl;
} else {
std::cout << "Something went wrong comparing the file with itself. :(" << std::endl;
return -1;
}
}
}