判斷該資料夾是否有 .c 的檔案

如果有就輸出它的名稱

 

#define _SVID_SOURCE
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <fnmatch.h>
#include <sys/types.h>
#include <strings.h>

int main(){

char *pattern;
struct dirent **namelist;
int i, n;
char *s;

n = scandir(".", &namelist, 0, alphasort); // all file is restore in the scandir
pattern = "*.c"; // the pattern we are looking for

for(i = 0; i < n; i++) {
if(fnmatch(pattern, namelist[i]->d_name, FNM_PATHNAME) == 0) {
s = namelist[i]->d_name;
*(rindex(s,'.')) = '\0';
printf("%s\n", s);
}
}
return 0;
}

arrow
arrow
    全站熱搜

    inmyworld 發表在 痞客邦 留言(0) 人氣()