Ads 468x60px

##EasyReadMore##

10 6月, 2014

C++ 列出目錄下的檔案及判別是否為目錄

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>


main() {
    DIR * dir;
    DIR * dir2;
    struct dirent * ptr;
    int i;
    dir =opendir(“/etc/rc.d”);


    //讀取檔案
    while((ptr = readdir(dir))!=NULL) {
        char pathname[100];
        sprintf(pathname,"/etc/rc.d/%s", ptr->d_name);
        if((dir2 = opendir(pathname))==NULL) { //判斷是否為目錄
            printf("%s: file\n", ptr->d_name);
        } else {
            printf("%s: directory\n", ptr->d_name);
            closedir(dir2);
        }
    }
    closedir(dir);
}


http://lazyflai.blogspot.tw/2009/01/c.html

0 意見:

張貼留言

 
Blogger Templates