批量读取文件对比内容,并将结果写入文件:
#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); //文件指针返回头部 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表示不同,1表示相同 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; }