SuperMonkey

fcomp

Sep 24th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <dirent.h>
  3. #include <cerrno>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. //dit si un fichier "fname" existe ou pas.
  11. bool fexists(char* fname) {
  12.     FILE* f = fopen(fname, "r");
  13.     if(errno == 2) {
  14.         return true;
  15.     }
  16.     fclose(f);
  17.     return false;
  18. }
  19.  
  20. size_t fsize(FILE* f) {
  21.     fseek(f, 0, SEEK_END);
  22.     size_t sz = ftell(f);
  23.     rewind(f);
  24.     return sz;
  25. }
  26.  
  27. bool fcomp(char* fn1, char* fn2) {
  28.     FILE *f1, *f2;
  29.     f1 = fopen(fn1, "r");
  30.     f2 = fopen(fn2, "r");
  31.    
  32.     if(f1 == NULL || f2 == NULL) return false;
  33.    
  34.     bool eq = true;
  35.     size_t fs1 = fsize(f1), fs2 = fsize(f2);
  36.     if(fs1 != fs2) {
  37.         fclose(f1);
  38.         fclose(f2);
  39.         return false;
  40.     }
  41.    
  42.     char buf[200], buf2[200];
  43.     while(!feof(f1) && !feof(f2) && eq) {
  44.         fread(buf, 1, 200, f1);
  45.         fread(buf2,1, 200, f2);
  46.         if(strcmp(buf, buf2)) {
  47.             eq = false;
  48.         }
  49.     }
  50.    
  51.     fclose(f1);
  52.     fclose(f2);
  53.    
  54.     return eq;
  55. }
  56.  
  57. int main()
  58. {
  59.     char* f1 = "fichier1.txt", *f2 = "fichier2.txt";
  60.     std::cout << "Les fichiers " << f1 << " et " << f2 << " sont " << ((fcomp(f1, f2)) ? "identiques" : "différents") << std::endl;
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment