小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Linux下目錄遍歷&結(jié)構(gòu)體dirent - 姜戲◥⊙▲⊙◤郎 - 博客園

 omcc 2011-01-10

Linux下目錄遍歷&結(jié)構(gòu)體dirent

 

在linux下遍歷某一目錄下內(nèi)容LINUX下歷遍目錄的方法一般是這樣的
打開(kāi)目錄->讀取->關(guān)閉目錄
相關(guān)函數(shù)是opendir -> readdir -> closedir,其原型如下:
#include <dirent.h>
DIR *opendir(const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);

簡(jiǎn)單列舉一例:

#include<dirent.h>    

  

        struct dirent* ent = NULL;
        DIR
*pDir;
        

       
if( (pDir=opendir("/home/test")) == NULL)
        {
                printf(
"open dir %s failed\n", pszBaseDir);
               
return false;
        }

       
while( (ent=readdir(pDir)) != NULL )
        {
              printf("the ent->d_reclen is%d the ent->d_type is%d the ent->d_name is%s\n", ent->d_reclen, ent->d_type, ent->d_name);  

         }


        closedir(pDir);

 

 

其中有一很關(guān)鍵的結(jié)構(gòu)體dirent:

代碼
On Linux, the dirent structure is defined as follows:

struct dirent {
ino_t d_ino;
/* inode number */
off_t d_off;
/* offset to the next dirent */
unsigned
short d_reclen; /* length of this record */
unsigned
char d_type; /* type of file */
char d_name[256]; /* filename */
};

其中inode表示存放的是該文件的結(jié)點(diǎn)數(shù)目(具體可了解linux下的文件系統(tǒng)),d_off 是文件在目錄中的編移,這兩個(gè)基本很少用。

d_type表示檔案類型:

01 enum
02 {
03     DT_UNKNOWN = 0,
04 # define DT_UNKNOWN DT_UNKNOWN
05     DT_FIFO = 1,
06 # define DT_FIFO DT_FIFO
07     DT_CHR = 2,
08 # define DT_CHR DT_CHR
09     DT_DIR = 4,
10 # define DT_DIR DT_DIR
11     DT_BLK = 6,
12 # define DT_BLK DT_BLK
13     DT_REG = 8,
14 # define DT_REG DT_REG
15     DT_LNK = 10,
16 # define DT_LNK DT_LNK
17     DT_SOCK = 12,
18 # define DT_SOCK DT_SOCK
19     DT_WHT = 14
20 # define DT_WHT DT_WHT
21 };

d_reclen認(rèn)為是紀(jì)錄的長(zhǎng)度,計(jì)算方式應(yīng)該是4(d_ino)+4(d_off)+2(d_reclen)+1(d_type)+1(補(bǔ)齊位)+4N(d_name會(huì)自動(dòng)補(bǔ)齊:1.jpg為8,12.jpg也為8,1234.jpg也為8,12345.jpg則為12);所以一般d_reclen是20和24(其中.和..是16)。

d_name表示文件名,如test.jpg

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多