Johnny talk Johnny way


opendir讀取目錄裡的檔案清單
2009, 四月 22, 星期三, 3:10 下午
分類: C Language | 標籤: , , , , , , ,

其實跟讀取檔案內容差不多, 一開始想用ls輸出檔案,再從檔案中來取得檔案清單,但是必須有可以使用空間的先決條件,如果用popen也覺得不太安全(在thread的狀況下使用),後來找到有現成的system call可以用,就是opendirreaddir,
還蠻簡單的, 請看下面的範例程式囉~


#include
#include

main(int argc, char **argv)
{
struct dirent *entry;

DIR *dh = opendir(path);

while ((entry = readdir(dh)) != NULL) {
printf("%s\n", entry->d_name);
}

closedir(dh);
}




Follow

Get every new post delivered to your Inbox.