相關函數(shù):scandir, opendir, readdir, alphasort
表頭文件:#include <dirent.h>
定義函數(shù):int scandir(const char *dir, struct dirent **namelist, nt (*select) (const struct dirent *), nt (*compar) (const struct dirent **, const struct dirent**));
函數(shù)說明:scandir()會掃描參數(shù)dir指定的目錄文件,經由參數(shù)select指定的函數(shù)來挑選目錄結構至參數(shù)namelist數(shù)組中,最后再調用參數(shù)compar指定的函數(shù)來排序namelist數(shù)組中的目錄數(shù)據。每次從目錄文件中讀取一個目錄結構后便將此結構傳給參數(shù)select所指的函數(shù), select函數(shù)若不想要將此目錄結構復制到namelist數(shù)組就返回0,若select為空指針則代表選擇所有的目錄結構。scandir()會調用qsort()來排序數(shù)據,參數(shù)compar則為qsort()的參數(shù),若是要排列目錄名稱字母則可使用alphasort(). 結構dirent定義請參考readdir()
返回值 :成功則返回復制到namelist數(shù)組中的數(shù)據結構數(shù)目,有錯誤發(fā)生則返回-1
錯誤代碼:ENOMEM 核心內存不足
/* 讀取 / 目錄下文件名長度大于5的目錄結構 */ #include <dirent.h>
int select(const struct dirent *dir) { if(strlen(dir->d_name) > 5) return 1; else return 0; }
main() { struct dirent **namelist; int i, total; if(total < 0) perror("scandir"); else { for(i=0; i<total; i++) printf("%s\n", namelist->d_name); printf("total = %d\n", total); free( namelist ); } }
-------------------------------------------------------------------------- struct dirent 目錄路徑定義: #include <dirent.h> struct dirent { long d_ino; /* inode number 索引節(jié)點號 */ 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字符 */ } |
下面是更詳細的定義,摘自:
http://www.delorie.com/gnu/docs/glibc/libc_270.html
This is a structure type used to return information about directory entries. It contains the following fields:
char d_name[]
- This is the null-terminated file name component. This is the only field you can count on in all POSIX systems.
ino_t d_fileno
- This is the file serial number. For BSD compatibility, you can also refer to this member as
d_ino
. In the GNU system and most POSIX systems, for most files this the same as the st_ino
member that stat
will return for the file. See section 14.9 File Attributes.
unsigned char d_namlen
- This is the length of the file name, not including the terminating null character. Its type is
unsigned char
because that is the integer type of the appropriate size
unsigned char d_type
- This is the type of the file, possibly unknown. The following constants are defined for its value:
DT_UNKNOWN
- The type is unknown. On some systems this is the only value returned.
DT_REG
- A regular file.
DT_DIR
- A directory.
DT_FIFO
- A named pipe, or FIFO. See section 15.3 FIFO Special Files.
DT_SOCK
- A local-domain socket.
DT_CHR
- A character device.
DT_BLK
- A block device.
This member is a BSD extension. The symbol _DIRENT_HAVE_D_TYPE
is defined if this member is available. On systems where it is used, it corresponds to the file type bits in the st_mode
member of struct statbuf
. If the value cannot be determine the member value is DT_UNKNOWN. These two macros convert between d_type
values and st_mode
values:
- Function: int IFTODT (mode_t mode)
- This returns the
d_type
value corresponding to mode.
- Function: mode_t DTTOIF (int dtype)
- This returns the
st_mode
value corresponding to dtype.
This structure may contain additional members in the future. Their availability is always announced in the compilation environment by a macro names _DIRENT_HAVE_D_xxx
where xxx is replaced by the name of the new member. For instance, the member d_reclen
available on some systems is announced through the macro _DIRENT_HAVE_D_RECLEN
.
When a file has multiple names, each name has its own directory entry. The only way you can tell that the directory entries belong to a single file is that they have the same value for the d_fileno
field.