dirent

dirent

dirent,LINUX系統下的一個頭檔案,在這個目錄下/usr/include,為了獲取某資料夾目錄內容,所使用的結構體

基本介紹

  • 外文名:dirent
  • 平台:LINUX系統
  • 類型:頭檔案
  • 目錄:/usr/include
語言編程,結構體說明,相關函式,使用實例,

語言編程

LINUX系統下的一個頭檔案,在這個目錄下/usr/include
為了獲取某資料夾目錄內容,所使用的結構體
引用頭檔案#include<dirent.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) 檔案名稱,最長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[])
{
static char dot[] =".", dotdot[] ="..";
const char* name;
DIR* dirp;
struct dirent*dp;
if (argc == 2)
name = argv[1];
else
name = dot;
if ((dirp = opendir(name))== NULL) {
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))
printf("%s/\n", dp->d_name);
}
closedir(dirp);
return 0;
}

相關詞條

熱門詞條

聯絡我們