2010年9月12日 星期日

[Linux] 列出資料夾下的檔案

#include (stdio.h) ->Blogger不支援<>,用()代替
#include (stdlib.h)
#include (string.h)
#include (dirent.h)

int main () {
struct dirent *dp;

const char *dir_path="/etc/";
DIR *dir = opendir(dir_path);
while ((dp=readdir(dir)) != NULL) {
char *tmp;
if(dp->d_type == DT_DIR ) //Sub directory
printf("%s: DIR\n", dp->d_name);
else if(dp->d_type == DT_REG)//Regular file.
printf("%s: FILE\n", dp->d_name);
}
closedir(dir);
return 0;
}

沒有留言: