linux 下遍历文件夹下的所有文件

CANoe诊断测试避坑指南:DTC故障注入与ECU刷写失败的N个常见原因排查 本文深入解析CANoe诊断测试中的常见问题,重点探讨DTC故障注入与ECU刷写失败的排查策略。从会话管理、安全访问到数据完整性校验,提供实战经验与CAPL脚本示例,帮助工程师规避诊断测试中的典型陷阱,提升车载网络通信与ECU刷写的可靠性。 阅读详情
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等等。


三个函数介绍完了,直接来一个例子吧:
**********************************************SearchDir.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;
}






//////////////////////////////////////////////////////////
Linux下C语言遍历文件夹


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


转CU,地址:


http://blog.chinaunix.net/u/29024/showart_484896.html


#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); 
 }
【通信】多径衰落信道的matlab仿真 1 简介 2 部分代码 clearf_c=1e3; %carrier frequency(no modulation)time_1 = (linspace (0, 10, 1000)); %timesignal_in = sin (2 * pi *f_c* time_1); %sine wave plot (time_1, signal_in, 'b');grid on; 阅读详情

相关推荐

我是如何让爬虫在小红书风控下“偷数据”的?xhs爬虫这些骚操作你可能想不到

最绝的是,他们连评论区都做了动态加载,第一页显示20条评论,再往下滑就变成“加载失败”,实际上得模拟真人反复上滑下滑才能触发完整加载。上个月我们团队帮一家美妆品牌抓竞品爆款笔记,刚开了10个爬虫账号,结果5分钟不到,系统直接弹窗警告“检测到非正常操作”——后来发现,问题出在Wi-Fi上。小红书的风控每周都在升级,上周还能用的方案,这周可能就得重写。但要是想大规模采集,比如监控竞品爆文、分析用户画像,劝你先算笔账:光是一个能抗封的账号矩阵,每月成本就得烧掉几万块,更别说还得养团队随时应对系统升级。

klj3388的博客 1万+

linux 遍历指定目录下所有文件

返回值:struct dirent * => 成功:返回目录结构体指针;失败或者到达目录流的末尾:返回 NULL。opendir函数的作用:打开一个给定的目录文件,并返回目录流指针。readdir函数的作用:读取一个指向。closedir函数的作用:关闭一个已经打开的目录流指针,与。返回值:DIR * => 成功:返回目录流指针;失败:返回 NULL。输入参数:name => 传入目录的路径名。输入参数:dirp => 打开的目录流指针,返回值:成功:返回 0;输入参数:dirp => 打开的目录流指针。

长龙的博客 1639

Lattice FPGA 开发工具Diamond使用流程总结——工具使用

本文介绍了一般的Diamond开发流程,包括工程创建、文件输入、IP使用、约束、管脚分配、和布线布局等流程

weixin_41895751的博客 2万+

【文件IO】遍历指定目录下的所有文件

Linux中遍历指定目录下的所有文件

challenglistic的博客 5722

数据集分享 | IWR1642呼吸心跳数据集

雷达开源数据库 数据集

【调皮连续波】雷达技术领域知识博主 3206

PCL 直通滤波

内容抄自优快云点云侠:[【2024最新版】PCL点云处理算法汇总(C++长期更新版)](https://blog.youkuaiyun.com/qq_36686437/article/details/114160640)。质量无忧,永久免费,可放心复制粘贴。

m0_51204289的博客 906

Linux遍历指定目录下所有匹配指定文件名的文件,并打印出每个文件的路径和文件内容

在我们的Linux编程过程中,我们经常会查找不同目录下的相同文件名,那我们怎样可以快速且清晰的查找到我们想获取的内容呢,我们可以执行下面的shell语句,而不需要写一个脚本。

代码背包客的博客 1487

Xilinx平台SRIO介绍(一)RapidIO协议介绍

菜鸟到老鸟:会用工具; 老鸟到大佬:深入理解原理。 ——FPGA大叔·沃自己硕得

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

Linux 目录遍历文件

【代码】Linux 目录遍历文件。

aningxiaoxixi的博客 527

【proteus】模拟串口和物理串口通讯

proteus提供了两种串口,一种是模拟的串口,还有一张是用软件仿真到硬件的物理串口。他们的区别就是:模拟串口只能在proteus内部进行通讯,但是物理可以通过软件映射到硬件上和单机或者串口软件通讯。 物理串口在元器件搜索COMPIM即可找到。 模拟串口在这里: PROTEUS仿真图: 物理串口COMPIM的设置: 模拟串口的设置: VSPD是一款虚拟串口软件,VSPD下载链接: 链接:https://pan.baidu.com/s/1AZygJCeWZfQTnxdCb_3TPg 提取码:12pm

阿蛋的博客 8935

Qt入门教程【基础控件篇】QLineEdit输入框

QLineEdit输入框

小鱼酱的博客 3264

LINUX 遍历获取文件夹下得所有文件 目录 (递归)

最近需要写个程序,处理一写日志。然后要用到遍历Linux下得所有文件和子目录这以招。看了很多,大同小异。写个自己得。 #include <iostream> #include <stdlib.h> #include <stdio.h> #include <dirent.h> #include <unistd.h> #include <vector> #include <string> #include <algor

Terminator的专栏 6624

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

linux下遍历文件夹下的文件

工作中经常用到遍历文件夹,下面的三个小函数可能会对您遍历文件夹提供一些思路,代码都是在linux下环境中测试通过的,Qt版和boost版理论上在windows也能编译通过,请自己测试使用。 一、linux C版 // 查找文件夹下的.XX格式的文件 list&lt;string&gt; getDirFile(const string dirPath,const char *extenSt...

ISmileLi的博客 5992

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

shellfor遍历文件夹_shell linux 环境下循环遍历文件夹下所有文件

【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 &vert; 包括特殊字符文件名的处理要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh input_path output_ ...Shell 命令行,写一个自动整理 ~&sol;Downl...

weixin_39607620的博客 3834

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

学习了LINUX下用C语言遍历文件夹,一些心得

Linux下C语言遍历文件夹[转] 2010-11-26 16:08:03|  分类: linux |  标签: |字号大中小 订阅 学习了LINUX下用C语言遍历文件夹,一些心得 struct dirent中的几个成员: d_type:4表示为目录,

kesaihao862的专栏 1万+

Linux 下 C++ 遍历文件夹下的文件名

在C++中,我们一般学的时候都很少学到相关的文件夹方面的操作,相对来说,C++的文件夹操作也比较麻烦。而不像Python有OS模块,在操作文件夹方面简单易于上手。 下面我们直接看一下,C++遍历文件夹的代码吧: #include <string> #include <dirent.h> #include <stdio.h> #include &lt...

Dylan的博客 5937

Linux C语言遍历文件夹

#include #include #include void List(char *path) { printf("路径为[%s]\n", path); struct dirent* ent = NULL; DIR *pDir; pDir=opendir(path); //d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文

蓝鸟云的博客 1303

linux遍历文件夹下所有文件大小,Linux系统遍历文件夹 获取文件大小的操作(C语言实现)...

/**linux 下遍历文件夹以及获取文件大小*/#include #include #include #include /** 获取文件大小,但是文件大小不能超过2G ,该方法不推荐使用 */unsigned long get_file_size_small(const char *filename){FILE *pFile = NULL;int iRet = 0;unsigned long u...

weixin_31156651的博客 812

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

深入探讨:linux中遍历文件夹下的所有文件 http://www.jb51.net/article/37664.htm

RICK 1257

Linux shell 遍历文件夹文件和目录的脚本

写一个遍历文件夹目录和文件的脚本,默认搜索文件,加上 -d 参数搜索目录,可以设置搜索深度,参数设置模仿find 命令,当然效率没有find命令高,日常使用还是使用find命令,此脚本仅做参考 #! /bin/bash function IsNumber { [ $# -eq 0 ] && return 1 echo $1 | grep -P "^\d+$" &gt...

爱读庄子的码农 2349

shellfor遍历文件夹_linux shell 遍历指定目录下的所有文件夹

在linux 中,如何遍历指定目录下的所有文件夹呢?要求能搜索结果中包含隐藏文件夹脚本名:ergodic_folder.sh脚本内容:#!/bin/shlist_alldir(){for file2 in `ls -a $1`doif [ x"$file2" != x"." -a x"$file2" != x".." ];thenif [ -d "$1/$file2" ];thenecho "$1...

weixin_42414212的博客 1678

linux下 c语言递归遍历文件夹下所有文件和子文件夹(附上替换文本文件内容的方法)

#include #include #include #include int IS_DIR(const char* path); //遍历文件夹的驱动函数 void List_Files(const char *path); //遍历文件夹de递归函数 void List_Files_Core(const char *path, int recursive); int main(in

zwcwu31的专栏 3540

linux无法便遍历文件夹下的文件

$hostdir = './UploadsCover/' . $y . '/' . $m . '/' . $d . '/'; //获取本文件目录的文件夹地址 $a = glob($hostdir."/*");dump($a);die(); 使用glob函数可以得到固定或者所有格式的文件*.* 这样就可以遍历了,因为linux目前不能直接访问文件夹,只能访问文件。

qq_30323757的博客 359

[转]Linux 下用C语言遍历文件夹

学习了LINUX下用C语言遍历文件夹,一些心得 struct dirent中的几个成员: d_type:4表示为目录,8表示为文件 d_reclen:16表示子目录或文件,24表示非子目录 d_name:目录或文件的名称 具体代码如下,仅供参考 #include #include #include void List(char *path) { str

茶_CC 546

linux shell 遍历指定目录下的所有文件夹

在linux 中,如何遍历指定目录下的所有文件夹呢? 要求能搜索结果中包含隐藏文件夹 脚本名:ergodic_folder.sh 脚本内容: #!/bin/sh list_alldir(){ for file2 in `ls -a $1` do if [ x"$file2" != x"." -a x"$file2" != x".." ];the...

hw1287789687的专栏 1047

Linux driver 遍历指定文件夹查找文件

linux 应用层可以使用opendir, readdir, closedir接口进行文件夹遍历. API接口位于 linux/fs/readdir.c   before 3.11 linux driver层, 则可以使用file_operation中的readdir(3.11以前)接口进行读取. typedef int (*filldir_t)(void *, const char ...

Lulixue的专栏 2143
上一篇: c语言中的线程池
下一篇: linux 中的top命令下的VIRT RES
dds_dev_group
博客等级 码龄15年 4粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值