Batch read comparison files

Read the file comparison contents in batches and write the results to the file:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
    int i;
    FILE *file1,*file2,*file;
    char ch1,ch2,path1_tail[2000]={0},path2_tail[2000]={0};
    char path[50]="/home/adu/Documents/project/result/v1";

    if((file=fopen(path,"a+"))!=NULL){
        for(i=1;i<=1052;i++){
            int count1=0,count2=0;
            int flag=0;
            char path1[50]="/home/adu/Documents/project/totinfo/outputs/t";
            char path2[50]="/home/adu/Documents/project/totinfo/newoutputs/t";
            sprintf(path1_tail,"%d",i);
            sprintf(path2_tail,"%d",i);
            strcat(path1,path1_tail);
            strcat(path2,path2_tail);
            if(((file1=fopen(path1,"r"))!=NULL)&&(file2=fopen(path2,"r"))!=NULL){
                while((ch1=getc(file1))!=EOF){
                    count1++;
                }
                while((ch2=getc(file2))!=EOF){
                    count2++;
                }
                rewind(file1); //The file pointer returns to the head
                rewind(file2);
                if(count1==count2) {
                    while (((ch1 = fgetc(file1)) != EOF) && (ch2 = fgetc(file2)) != EOF) {
                        if (ch1 == ch2) {
                            //continue;
                        } else if (ch1 != ch2) {
                            fputc('0', file); //0 means different, 1 means the same
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1) {
                        fclose(file1);
                        fclose(file2);
                        //break;
                    }
                    else{
                        fclose(file1);
                        fclose(file2);
                        fputc('1',file);
                    }
                }
                else{
                    fputc('0',file);
                }
            }
            else{
               printf("faile:%d\n",i);
               continue;
            }
        }
        fclose(file);
        printf("%d",i);
        printf("it`s over");
    }
    else{
      printf("file open failed");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517819&siteId=291194637