close

Unix C 練習程式

 

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

int main() {
struct dirent **namelist;
struct stat sb;
int num = scandir(".", &namelist, NULL, alphasort);
while (num--) {
if (strcmp(namelist[num]->d_name, ".") == 0) {
continue;
} else if (strcmp(namelist[num]->d_name, "..") == 0) {
continue;
}

if (stat(namelist[num]->d_name, &sb) == -1) {
perror("stat");
exit(1);
} else if (S_ISDIR(sb.st_mode)) {
printf("This is DIR %s\n", namelist[num]->d_name);
//check_dir(namelist[num]->d_name);
} else {
printf("This is FILE %s\n", namelist[num]->d_name);
}
free(namelist[num]);
}
free(namelist);
return 0;
}

 

------------------------------------------------------------------------

 

printf 會把所有的檔案列出來 並判斷是文件或是資料夾

arrow
arrow
    全站熱搜

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