2011-01-19 01:11

[C語言] 比對兩個檔案中的 list

這是用很簡單的方式比對兩個檔案中的 list
並不是很有效率就是了,只是希望比 shell 快一點

當 diffPath 檔案中的項目不在 basePath 的檔案裡,就會 print 出來。

#include <stdio.h>
#include <errno.h>
#include <string.h>

#define BUFFER_SIZE     300    /*char buffer size*/

int main(int argc, char *argv[]) {
    if(argc !=3 ){
        fprintf(stderr,"%s [diffPath] [basePath]\n",argv[0]); return 1;
    }

    FILE *diffFile, *baseFile;
    char diffChars[BUFFER_SIZE], baseChars[BUFFER_SIZE];

    if((diffFile=fopen(argv[1], "r")) == NULL){
        fprintf(stderr,"can't open diff file\n"); return 1;
    }
    if((baseFile=fopen(argv[2], "r")) == NULL){
        fprintf(stderr,"can't open base file\n"); return 1;
    }

    while(!feof(diffFile)){
        /* read diff item */
        fscanf(diffFile,"%s\n",diffChars);
    
        int isFind=0;
        /* find item exist */
        rewind(baseFile);
        while(!feof(baseFile)){
            fscanf(baseFile,"%s\n",baseChars);
            if(strcmp(diffChars,baseChars) == 0){ isFind=1; break; }
        }

        /* not find itme */
        if(!isFind){
            printf("%s\n",diffChars);
        }
    }

    fclose(diffFile);
    fclose(baseFile);

    return 0;
}

0 回應: