Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <dirent.h>
- #include <cerrno>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- //dit si un fichier "fname" existe ou pas.
- bool fexists(char* fname) {
- FILE* f = fopen(fname, "r");
- if(errno == 2) {
- return true;
- }
- fclose(f);
- return false;
- }
- size_t fsize(FILE* f) {
- fseek(f, 0, SEEK_END);
- size_t sz = ftell(f);
- rewind(f);
- return sz;
- }
- bool fcomp(char* fn1, char* fn2) {
- FILE *f1, *f2;
- f1 = fopen(fn1, "r");
- f2 = fopen(fn2, "r");
- if(f1 == NULL || f2 == NULL) return false;
- bool eq = true;
- size_t fs1 = fsize(f1), fs2 = fsize(f2);
- if(fs1 != fs2) {
- fclose(f1);
- fclose(f2);
- return false;
- }
- char buf[200], buf2[200];
- while(!feof(f1) && !feof(f2) && eq) {
- fread(buf, 1, 200, f1);
- fread(buf2,1, 200, f2);
- if(strcmp(buf, buf2)) {
- eq = false;
- }
- }
- fclose(f1);
- fclose(f2);
- return eq;
- }
- int main()
- {
- char* f1 = "fichier1.txt", *f2 = "fichier2.txt";
- std::cout << "Les fichiers " << f1 << " et " << f2 << " sont " << ((fcomp(f1, f2)) ? "identiques" : "différents") << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment