需要比较两个文件中的配置是否一致,需要找出另一个文件中不包含的配置;
当配置项非常多,且相同配置项错乱时,compare比较久显的不好用了;
因此写了一个小demo来快速比较,如下借鉴,
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char **argv) {
if(3<argc) {
printf("usage: files1 files2\n");
return 0;
}
char *path_file1=NULL;
char *path_file2=NULL;
path_file1=argv[1];
path_file2=argv[2];
FILE *pfile1=fopen(path_file1,"r");
FILE *pfile2=fopen(path_file2,"r");
#define buf_max_len 256
char buf1[buf_max_len];
char buf2[buf_max_len];
int read_len1=0;
int read_len2=0;
int read_cnt1=0;
int read_cnt2=0;
char *read_ret=NULL;
fseek(pfile1,0,SEEK_SET);
read_ret=fgets(buf1,buf_max_len,pfile1);
read_len1=strnlen(buf1,buf_max_len);
buf1[read_len1-1]=0;
read_cnt1=1;
while(NULL!=read_ret) {
if('#'==buf1[0] || '\n'==buf1[0]) {
} else {
fseek(pfile2,0,SEEK_SET);
read_ret=fgets(buf2,buf_max_len,pfile2);
read_len2=strnlen(buf2,buf_max_len);
buf2[read_len2-1]=0;
read_cnt2=1;
while(NULL!=read_ret) {
if('#'==buf2[0] || '\n'==buf2[0]) {
} else {
if(0!=strncmp(buf1,buf2,buf_max_len)) {
// printf("next line %d,%s\n",read_cnt2,buf2);
} else {
// printf("%s,%s compatible %s,%s\n",path_file1,buf1,path_file2,buf2);
break;
}
}
read_ret=fgets(buf2,buf_max_len,pfile2);
read_len2=strnlen(buf2,buf_max_len);
buf2[read_len2-1]=0;
++read_cnt2;
}
if(NULL==read_ret) {
// printf("%s,%s not found compatible\n",path_file1,buf1);
printf("%6d,%s not found compatible\n",read_cnt1,buf1);
}
}
read_ret=fgets(buf1,buf_max_len,pfile1);
read_len1=strnlen(buf1,buf_max_len);
buf1[read_len1-1]=0;
++read_cnt1;
}
fclose(pfile1);
fclose(pfile2);
return 0;
}

1187

被折叠的 条评论
为什么被折叠?



