2011-01-19

[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;
}

沒有留言:

張貼留言

你好!歡迎你在我的 Blog 上留下你寶貴的意見。