Please help me on this question, the two files are in the argument where we need
ID: 3885293 • Letter: P
Question
Please help me on this question, the two files are in the argument where we need to open in Binary mode, and then read the file into memory, then compare if the two files are identical or not. You may only use three #include that are list above.
I do rate the answer, so please answer it as accurate as possible.
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 2017 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.Explanation / Answer
// Put appropriate file names in main function
#include<iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
bool compareFiles(const std::string& p1, const std::string& p2) {
std::ifstream f1(p1.c_str(), std::ifstream::binary|std::ifstream::ate);
std::ifstream f2(p2.c_str(), std::ifstream::binary|std::ifstream::ate);
if (f1.fail() || f2.fail()) {
return false; //file problem
}
if (f1.tellg() != f2.tellg()) {
return false; //size mismatch
}
//seek back to beginning and use std::equal to compare contents
f1.seekg(0, std::ifstream::beg);
f2.seekg(0, std::ifstream::beg);
return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()),
std::istreambuf_iterator<char>(),
std::istreambuf_iterator<char>(f2.rdbuf()));
}
int main(){
cout<<compareFiles("E:\hobbies.txt", "E:\hobbies2.txt");
return 0;
}