C語言readdir()函數:讀取目錄函式

來源:互聯網
上載者:User
相關函數:open, opendir, closedir, rewinddir, seekdir, telldir, scandir

標頭檔:#include <sys/types.h>   #include <dirent.h>

定義函數:struct dirent * readdir(DIR * dir);

函數說明:readdir()返回參數dir 目錄流的下個目錄進入點。結構dirent 定義如下:
struct dirent
{
    ino_t d_ino; //d_ino 此目錄進入點的inode
    ff_t d_off; //d_off 目錄檔案開頭至此目錄進入點的位移
    signed short int d_reclen; //d_reclen _name 的長度, 不包含NULL 字元
    unsigned char d_type; //d_type d_name 所指的檔案類型 d_name 檔案名稱
    har d_name[256];
};

傳回值:成功則返回下個目錄進入點. 有錯誤發生或讀取到目錄檔案尾則返回NULL.

附加說明:EBADF 參數dir 為無效的目錄流。

範例
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
main()
{
    DIR * dir;
    struct dirent * ptr;
    int i;
    dir = opendir("/etc/rc.d");
    while((ptr = readdir(dir)) != NULL)
    {
        printf("d_name : %s\n", ptr->d_name);
    }
    closedir(dir);
}
執行:
d_name : .
d_name : ..
d_name : init.d
d_name : rc0.d
d_name : rc1.d
d_name : rc2.d
d_name : rc3.d
d_name : rc4.d
d_name : rc5.d
d_name : rc6.d
d_name : rc
d_name : rc.local
d_name : rc.sysinit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.