2011-03-28

[C語言] 用 fnmatch 依副檔名過濾檔案類型

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

#define SIZE 256

int main(int argc, const char ** argv){
    char extension[] = "jpg,png,gif";

    char extensionTemp[SIZE];
    char pattern[SIZE];
    char *token;

    for (int i = 1; i < argc; i++){
        strcpy(extensionTemp,extension);
        token = strtok(extensionTemp,",");
        while (token != NULL && strlen(token)>0){
            strcpy (pattern,"*."); // *.
            strcat (pattern,token); // *.jpg

            if( fnmatch(pattern,argv[i],0) == 0 ) {
                printf("%s\n",argv[i]);
            }

            token = strtok(NULL,",");
        }
    }

    return 0;
}

0 回應: