Linux C 读取文件夹下所有文件(包括子文件夹)

递归读取某文件夹及其子文件夹下所有文件名

Linux C 下面读取文件夹要用到结构体struct dirent,在头#include <dirent.h>中,如下:

#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) 文件名,最长255字符 */
}

其中d_type表明该文件的类型:文件(8)、目录(4)、链接文件(10)等。

下面程序,递归读取某文件夹及其子文件夹下所有文件名:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
int readFileList(char *basePath)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8)    ///file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
        else if(ptr->d_type == 10)    ///link file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
        else if(ptr->d_type == 4)    ///dir
        {
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            readFileList(base);
        }
    }
    closedir(dir);
    return 1;
}

int main(void)
{
    DIR *dir;
    char basePath[1000];

    ///get the current absoulte path
    memset(basePath,'\0',sizeof(basePath));
    getcwd(basePath, 999);
    printf("the current dir is : %s\n",basePath);

    ///get the file list
    memset(basePath,'\0',sizeof(basePath));
    strcpy(basePath,"./XL");
    readFileList(basePath);
    return 0;
}

深入探讨:linux中遍历文件夹下的所有文件

linux C 遍历目录及其子目录

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
void listDir(char *path)
{
        DIR              *pDir ;
        struct dirent    *ent  ;
        int               i=0  ;
        char              childpath[512];

        pDir=opendir(path);
        memset(childpath,0,sizeof(childpath));

        while((ent=readdir(pDir))!=NULL)
        {
                if(ent->d_type & DT_DIR)
                {
                 		if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
                                continue;
                        sprintf(childpath,"%s/%s",path,ent->d_name);
                        printf("path:%s/n",childpath);

                        listDir(childpath);
                }
				else
				{
					cout<<ent->d_name<<endl;
				}
        }
}

int main(int argc,char *argv[])
{
        listDir(argv[1]);
        return 0;
}

Linux C :遍历输出指定目录下的所有文件

在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录。在使用这三个函数前必须先包括以下两个头文件:

#include <sys/types.h>
#include <dirent.h>

opendir函数的原型为:

DIR *opendir(const char *name);

它返回一个DIR*类型,这就是一个句柄啦,你不用管它的内部结构是什么样的,只要知道这个句柄就是等一下要传给readdir()函数的参数就行了。
readdir函数的原型为:

struct dirent *readdir(DIR *dir);

看它的参数就知道该参数是opendir函数返回的句柄,而该函数的返回值是struct dirent* 类型,这里我们必须了解一下这个结构体:

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 */
};

这个结构体的d_name存放的就是文件的名字,这里的文件包括普通文件,目录文件等等,在linux的思想中,所有的东西都是文件。
closedir函数的原型为:
int closedir(DIR *dir);
这个函数就不用多说了,一般有开(open),就有关(close),这样的结构经常可出看到,如fopen,fclose等等。
三个函数介绍完了,直接来一个例子吧:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
char filename[256][256];
int len = 0;
int trave_dir(char* path, int depth)
{
    DIR *d; //声明一个句柄
    struct dirent *file; //readdir函数的返回值就存放在这个结构体中
    struct stat sb;

    if(!(d = opendir(path)))
    {
        printf("error opendir %s!!!\n",path);
        return -1;
    }
    while((file = readdir(d)) != NULL)
    {
        //把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
        if(strncmp(file->d_name, ".", 1) == 0)
            continue;
        strcpy(filename[len++], file->d_name); //保存遍历到的文件名
        //判断该文件是否是目录,及是否已搜索了三层,这里我定义只搜索了三层目录,太深就不搜了,省得搜出太多文件
        if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 3)
        {
            trave_dir(file->d_name, depth + 1);
        }
    }
    closedir(d);
    return 0;
}
int main()
{
    int depth = 1;
    int i;
    trave_dir("/usr/keygoe/ini/", depth);
    for(i = 0; i < len; i++)
    {
        printf("%s\t", filename[i]);
    }
    printf("\n");
    return 0;
}

Linux下C语言遍历文件夹

struct dirent中的几个成员:
d_type:4表示为目录,8表示为文件
d_reclen:16表示子目录或文件,24表示非子目录
其他,d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,等等
d_name:目录或文件的名称
具体代码如下,仅供参考

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
void List(char *path)
{
     struct dirent* ent = NULL;
     DIR *pDir;
     pDir=opendir(path);
     while (NULL != (ent=readdir(pDir)))
     {
         if (ent->d_reclen==24)
         {
             if (ent->d_type==8)
             {
                 printf("普通文件:%s\n", ent->d_name);
             }
             else
             {
                 printf("子目录:%s\n",ent->d_name);
                 List(ent->d_name);
                 printf("返回%s\n",ent->d_name);
             }
         }
     }
}
int main(int argc, char *argv[])
{
      List(argv[1]);
      return 0;
}

上面函数修改后:

void List(char *path)
{
     printf("路径为[%s]\n", path);

     struct dirent* ent = NULL;
     DIR *pDir;
     pDir=opendir(path);
     //d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,还有其他……
     while (NULL != (ent=readdir(pDir)))
     {
         printf("reclen=%d    type=%d\t", ent->d_reclen, ent->d_type);
         if (ent->d_reclen==24)
         {
             //d_type:4表示为目录,8表示为文件
             if (ent->d_type==8)
             {
                 printf("普通文件[%s]\n", ent->d_name);
             }
         }
         else if(ent->d_reclen==16)
         {
             printf("[.]开头的子目录或隐藏文件[%s]\n",ent->d_name);
         }
         else
         {
             printf("其他文件[%s]\n", ent->d_name);
         }
     }
}
#include   <stdio.h>
#include   <dirent.h>
#include   <sys/types.h>
#include   <sys/stat.h>

void dir_scan(char   *path,   char   *file);
int count = 0;

int main(int   argc,   char   *argv[])
{
                   struct   stat   s;

                   if(argc   !=   2){
                                   printf("one   direction   requried\n");
                                   exit(1);
                   }
                   if(lstat(argv[1],   &s)   <   0){
                                   printf("lstat   error\n");
                                   exit(2);
                   }
                  //判断一个路径是否是目录
                   if(!S_ISDIR(s.st_mode)){
                                   printf("%s   is   not   a   direction   name\n",   argv[1]);
                                   exit(3);
                   }

                   dir_scan("",   argv[1]);

                   printf("total:   %d   files\n",   count);

                   exit(0);
}

void   dir_scan(char   *path,   char   *file)
{
                   struct   stat   s;
                   DIR           *dir;
                   struct   dirent   *dt;
                   char   dirname[50];

                   memset(dirname,   0,   50*sizeof(char));
                   strcpy(dirname,   path);

                   if(lstat(file,   &s)   <   0){
                                   printf("lstat   error\n");
                   }

                   if(S_ISDIR(s.st_mode)){
                                   strcpy(dirname+strlen(dirname),   file);
                                   strcpy(dirname+strlen(dirname),   "/");
                                   if((dir   =   opendir(file))   ==   NULL){
                                                   printf("opendir   %s/%s   error\n");
                                                   exit(4);
                                   }
                                   if(chdir(file)   <   0)   {
                                                   printf("chdir   error\n");
                                                   exit(5);
                                   }
                                   while((dt   =   readdir(dir))   !=   NULL){
                                                   if(dt->d_name[0]   ==   '.'){
                                                                   continue;
                                                   }

                                                   dir_scan(dirname,   dt->d_name);
                                   }
                                   if(chdir("..")   <   0){
                                                   printf("chdir   error\n");
                                                   exit(6);
                                   }
                   }else{
                                   printf("%s%s\n",   dirname,   file);
                                   count++;
                   }
}

linux c 下如何获得目录下的文件数目

int main(int argc, char **argv)
{
      DIR  * pdir;
     struct dirent * pdirent;
     struct stat f_ftime;
     int fcnt;/*文件数目统计*/
      pdir=opendir("./");
     if(pdir==NULL)
     {      return(-1);    }
      fcnt=0;
     for(pdirent=readdir(pdir);pdirent!=NULL;pdirent=readdir(pdir))
     {
       if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue;
       if(stat(pdirent->d_name,&f_ftime)!=0) return -1 ;
       if(S_ISDIR(f_ftime.st_mode)) continue; /*子目录跳过*/
        fcnt++;
       printf("文件:%s\n",pdirent->d_name);
     }
     printf("文件总数%d\n",fcnt);
      closedir(pdir);
     return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void printdir(char *dir, int depth)
{
          DIR *dp;
          struct dirent *entry;
          struct stat statbuf;

          if((dp = opendir(dir)) == NULL) {
                      fprintf(stderr, "cannot open directory: %s\n ", dir);
                      return;
          }
          chdir(dir);
          while((entry = readdir(dp)) != NULL) {
                      lstat(entry-> d_name,&statbuf);
                      if(S_ISDIR(statbuf.st_mode)) {
                                  /**//* Found a directory, but ignore . and .. */
                                  if(strcmp( ". ",entry-> d_name) == 0 ||
                                              strcmp( ".. ",entry-> d_name) == 0)
                                              continue;
                                  printf( "%*s%s/\n ",depth, " ",entry-> d_name);
                                  /**//* Recurse at a new indent level */
                                  printdir(entry-> d_name,depth+4);
                      }
                      else printf( "%*s%s\n ",depth, " ",entry-> d_name);
          }
          chdir( ".. ");
          closedir(dp);
}

/**//*    Now we move onto the main function.    */

int main(int argc, char* argv[])
{
          char *topdir, pwd[2]= ". ";
          if (argc != 2)
                      topdir=pwd;
          else
                      topdir=argv[1];

          printf( "Directory scan of %s\n ",topdir);
          printdir(topdir,0);
          printf( "done.\n ");

          exit(0);
}

关于readdir返回值中struct dirent.d_type的取值问题:

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. Only some filesystems have full support to return the type of the file, others might always return this value.

     类型未知。少数文件系统会出现此函数不支持的文件类型,另一些则总是返回这个值。译者注:总之这个值是为了应对不兼容的文件系统而设置的。


DT_REG
    A regular file.

    常规文件


DT_DIR
    A directory.

    目录


DT_FIFO
    A named pipe, or FIFO. See FIFO Special Files.

    一个命名管道,或FIFO。


DT_SOCK
    A local-domain socket.

    套接字


DT_CHR
    A character device.

     字符设备


DT_BLK
    A block device.

     块设备


DT_LNK
    A symbolic link. 

     符号链接

具体数值

enum
{ 
    DT_UNKNOWN = 0, 
 # define DT_UNKNOWN DT_UNKNOWN 
     DT_FIFO = 1, 
 # define DT_FIFO DT_FIFO 
     DT_CHR = 2, 
 # define DT_CHR DT_CHR 
     DT_DIR = 4, 
 # define DT_DIR DT_DIR 
     DT_BLK = 6, 
 # define DT_BLK DT_BLK 
     DT_REG = 8, 
 # define DT_REG DT_REG 
     DT_LNK = 10, 
 # define DT_LNK DT_LNK 
     DT_SOCK = 12, 
 # define DT_SOCK DT_SOCK 
     DT_WHT = 14 
 # define DT_WHT DT_WHT 
}; 

只用dirent存在问题:

部分linux文件系统,例如xfs不支持d_type,当你用d_type时,所有文件/目录的d_type会一直为空,即0(UNKNOWN),无法判断文件类型。所以需要stat

转载:Linux C 读取文件夹下所有文件(包括子文件夹)的文件名

  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值