1. 程式人生 > >linux c/c++ 讀取指定目錄下的檔名

linux c/c++ 讀取指定目錄下的檔名

#include <dirent.h>
#include <stdio.h>
/*struct dirent
{
   long d_ino; // inode number 索引節點號
   off_t d_off; // offset to this dirent 在目錄檔案中的偏移
   unsigned short d_reclen; // length of this d_name 檔名長
   unsigned char d_type; // the type of d_name 檔案型別
   char d_name [NAME_MAX+1]; // file name (null-terminated) 檔名,最長255字元
}
其中d_type表明該檔案的型別:檔案(8)、目錄(4)、連結檔案(10)等
*/
int main(){
    DIR *directory_pointer;
    struct dirent *entry;
    if((directory_pointer=opendir("/home/libin/桌面"))==NULL){
        printf("Error open\n");
        return ;
    } else {
        while((entry=readdir(directory_pointer))!=NULL){
            if(entry->d_name[0]=='.') continue;
            printf("%s\n",entry->d_name);
        }
    }
    return 0;
}