遍历文件夹下文件

最近有一个任务要做WEBUI上管理SD卡的内容,至少要可以显示SD卡内容和下载。

先把任务分解,至少三点

1、显示SD卡内容

2、下载功能

3、与webs通信

再看最基本的第一点,显示SD卡内容,那么重点是对文件夹下的内容进行遍历查找。

通过查找资料认识了这两个函数:

DIR * opendir(const char * name);
opendir()用来打开参数name 指定的目录, 并返回DIR*形态的目录流, 和open()类似, 接下来对目录的读取和搜索都要使用此返回值.

struct dirent* readdir(DIR* dir_handle);
本函数用来读取目录。返回是dirent结构体指针

dirent结构体成员如下,

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:
   DT_UNKNOWN,未知的类型
   DT_REG,普通文件
   DT_DIR,普通目录
   DT_FIFO,命名管道或FIFO
   DT_SOCK,本地套接口
   DT_CHR,字符设备文件
   DT_BLK,块设备文件

在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录,注意不要忘了closedir()。在使用这三个函数前必须先包括以下两个头文件:
#include <sys/types.h>
#include <dirent.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); //保存遍历到的文件名

    }
    closedir(d);//不要忘记了
    return 0;
}
int main()
{
    int depth = 1;
    int i;
    trave_dir("/media/card", depth);
    for(i = 0; i < len; i++)
    {
        printf("%s\t", filename[i]);
    }
    printf("\n");
    return 0;
}

原型是:

#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\n");

       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("/home/user/", depth);

   for(i = 0; i < len; i++)
   {
       printf("%s\t", filename[i]);
   }
   printf("\n");
   return 0;
}


编译成可执行文件,扔进根文件系统的/usr/bin目录下,运行,果然列出了SD卡里面的内容,第一小步成功,记录一下。

*************************************************************************************************************

以下内容转自:http://blog.csdn.net/wallwind/article/details/7528474,进行了简单的格式整理,未验证和研读代码

*****************

1、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; 
}

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

#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;
}

3、
学习了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;
}

4、上面函数修改后:

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);
         }
     }
}


5、
#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++;  
                   }  
}

6、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;
}



7、
#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);
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值