The check_same_content()
function takes two file paths as input and opens them in binary mode using std::ios::binary
flag. It then uses std::equal()
algorithm to compare the contents of both files, reading them in using std::istreambuf_iterator
. If the contents are the same, the function returns true
; otherwise, it returns false
. In the main()
function, we call check_same_content()
and print the result.
#include <iostream>
#include <fstream>
#include <string>
bool check_same_content(std::string file1, std::string file2) {
std::ifstream f1(file1, std::ios::binary);
std::ifstream f2(file2, std::ios::binary);
return std::equal(std::istreambuf_iterator<char>(f1.rdbuf()), std::istreambuf_iterator<char>(), std::istreambuf_iterator<char>(f2.rdbuf()));
}
int main() {
if (check_same_content("file1.txt", "file2.txt")) {
std::cout << "The files have the same content\n";
} else {
std::cout << "The files have different content\n";
}
return 0;
}