• 比较两个文件是否相同(C/C++语言)


      1 #include <stdio.h>
      2 #include <string.h>
      3 const int max_r = 1024;
      4 
      5 // Calculate the file size
      6 void Get_file_size(char *name_1, char *name_2)
      7 {
      8     FILE *fp_1 = fopen(name_1, "r");
      9     FILE *fp_2 = fopen(name_2, "r");
     10     if (fp_1)
     11     {
     12         printf ("Size of %s : ", name_1);
     13         fseek(fp_1, 0,  SEEK_END);
     14         if (1.*ftell(fp_1)/(1024*1024*1024) > 1.0)
     15                printf("%.1f GB
    ", 1.*ftell(fp_1)/(1024*1024*1024));
     16            else if(1.*ftell(fp_1)/(1024*1024) > 1.0)
     17                printf("%.1f MB
    ", 1.*ftell(fp_1)/(1024*1024));
     18            else if (1.*ftell(fp_1)/(1024) > 1.0)
     19                printf("%.1f KB
    ", 1.*ftell(fp_1)/(1024));
     20            else printf ("%d Bt
    ", ftell(fp_1));
     21         fclose(fp_1);
     22     }
     23     if (fp_2)
     24     {
     25         printf ("Size of %s : ", name_2);
     26         fseek(fp_2, 0,  SEEK_END);
     27         if (1.*ftell(fp_2)/(1024*1024*1024) > 1.0)
     28                printf("%.1f GB
    ", 1.*ftell(fp_2)/(1024*1024*1024));
     29            else if(1.*ftell(fp_2)/(1024*1024) > 1.0)
     30                printf("%.1f MB
    ", 1.*ftell(fp_2)/(1024*1024));
     31            else if (1.*ftell(fp_2)/(1024) > 1.0)
     32                printf("%.1f KB
    ", 1.*ftell(fp_2)/(1024));
     33            else printf ("%d Bt
    ", ftell(fp_2));
     34         fclose(fp_2);
     35     }
     36     return;
     37 }
     38 
     39 int main()
     40 {
     41     int AC, line_1, line_2;
     42     char name_1[100], name_2[100];
     43     char ch_1[max_r], ch_2[max_r];
     44     
     45     scanf ("%s%s", name_1, name_2);//input two file which need to be compared 
     46     AC = 1;
     47     line_1 = line_2 = 0;
     48     Get_file_size(name_1, name_2);
     49     
     50     FILE *fp_1 = fopen(name_1, "r");
     51     FILE *fp_2 = fopen(name_2, "r");
     52     while(1)
     53     {
     54         char *end_1, *end_2;
     55         end_1 = fgets(ch_1, max_r, fp_1);
     56         end_2 = fgets(ch_2, max_r, fp_2);
     57         if (end_1 != NULL)
     58             line_1 ++;
     59         if (end_2 != NULL)
     60             line_2 ++;
     61         while(strcmp(ch_1, "
    ") == 0 && end_1 != NULL)
     62         {
     63             end_1 = fgets(ch_1, max_r, fp_1);
     64             if (end_1 != NULL)
     65                 line_1 ++;
     66         }
     67         while(strcmp(ch_2, "
    ") == 0 && end_2 != NULL)
     68         {
     69             end_2 = fgets(ch_2, max_r, fp_2);
     70             if (end_2 != NULL)
     71                 line_2 ++;
     72         }
     73         if (end_1 == NULL && end_2 == NULL)
     74             break;
     75         if(strcmp(ch_1, ch_2) != 0)
     76         {
     77             AC = 0;
     78             ch_1[strlen(ch_1) -1] = '';
     79             ch_2[strlen(ch_2) -1] = '';
     80             printf ("    Worry Answer
    ");
     81             printf ("In file %s at %d line << %s >> difference from
    ", name_1, line_1, ch_1);
     82             printf ("In file %s at %d line << %s >>
    ", name_2, line_2, ch_2);
     83             break;
     84         }
     85     }
     86     if (AC)
     87     {
     88         if (line_1 != line_2)
     89         {
     90             printf ("    Print Error
    ");
     91             printf ("The print not in a canonical format!
    ");
     92             printf ("the %s have %d lines 
    but %s have %d lines!
    ", name_1, line_1, name_2, line_2);
     93         }
     94         else
     95             printf ("    Aceept
    ", line_1, line_2);
     96     }
     97     fclose(fp_1);
     98     fclose(fp_2);
     99     return 0;
    100 }
  • 相关阅读:
    Advanced Office Password Recovery 3.04 Professional Edition
    免费的ISO文件创建工具!
    opera for windows 上的阿里旺旺调用
    还不错,字母成熟了些!
    360的报应:超级巡警发布通用软件卸载工具 暂时只支持360
    未名晓店
    aMSN/QQ for Ubuntu 10.04
    IDENTITY
    最简单的bug管理系统
    代理网站
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3418535.html
Copyright © 2020-2023  润新知