dirent結構體

dirent,LINUX系統下的一個頭文件,在這個目錄下/usr/include,爲了獲取某文件夾目錄內容,所使用的結構體

1Linux下c語言編程所引用編輯

LINUX系統下的一個頭文件,在這個目錄下/usr/include
爲了獲取某文件夾目錄內容,所使用的結構體
引用頭文件#include<dirent.h>

2結構體說明編輯

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) 文件名,最長256字符 */
}

相關函數

opendir(),readdir(),closedir();

使用實例

#include <stdio.h> #include <errno.h>
#include <string.h> #include <sys/types.h>
#include <dirent.h>
#ifndef DT_DIR
#error "DT_DIR not defined, maybe d_type not a mumber of struct dirent!"
#endif
int
main(int argc, char*argv[])
{
staticchar dot[] =".", dotdot[] ="..";
constchar*name;
DIR *dirp;
struct dirent *dp;
if (argc ==2)
name = argv[1];
else
name = dot;
dirp = opendir(name);
if (dirp == NULL) {
(void)fprintf(stderr, "%s: opendir(): %s: %s\n",
argv[0], name, strerror(errno));
exit(errno);
}
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_type == DT_DIR)
if ( strcmp(dp->d_name, dot)
&& strcmp(dp->d_name, dotdot) )
(void)printf("%s/\n", dp->d_name);
}
(void)closedir(dirp);
return (0);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章