递归降序遍历目录层次结构,并按文件类型计数。

  先介绍相关的函数:

#include<dirent.h>

DIR *opendir(const char *pathname);        //打开目录
返回值:成功返回指针,出错返回NULL struct dirent *readdir(DIR *dp); //读取目录
返回值:成功返回指针,出错返回NULL void rewinddir(DIR *dp); //重设读取目录的位置为开头位置 int closedir(DIR *dp); //关闭目录
返回值:成功返回0,出错返回- long telldir(DIR *dp); //取得目录流的读取位置
返回值:与dp关联的目录中的当前位置 void seekdir(DIR *dp, long loc); //设置下回读取目录的位置 struct dirent{
ino_t d_ino; //i-node number
char d_name[NAME_MAX + ]; //null-terminated filename
}

  改写APUE的代码:

 #include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<string.h>
#include<stdlib.h> static int nreg = , ndir = , nblk = , nchr = , nfifo = , nslink = , nsock = , ntot = ; static char *fullpath; int myfunc(const char *pathname, const struct stat *statptr, int type)
{
switch(type){
case :
switch(statptr->st_mode & S_IFMT){
case S_IFREG: nreg++; break;
case S_IFBLK: nblk++; break;
case S_IFCHR: nchr++; break;
case S_IFIFO: nfifo++; break;
case S_IFLNK: nslink++; break;
case S_IFSOCK: nsock++; break;
case S_IFDIR:{
printf("for S_IFDIR for %s\n",pathname);
}
}
break;
case : //文件夹
ndir++; break;
case :
printf("cant read directory %s\n",pathname);
case :
printf("stat error for %s\n",pathname);
} return ;
} int dopath()
{
struct stat statbuf; //文件信息
struct dirent *dirp; //文件夹信息,包括i-node number filename
DIR *dp; //打开目录返回的指针
char *ptr; if((lstat(fullpath,&statbuf)) < )
return(myfunc(fullpath,&statbuf,));
if(S_ISDIR(statbuf.st_mode) == ) //判断是否是文件夹
return(myfunc(fullpath,&statbuf,)); myfunc(fullpath,&statbuf,); //是目录则调用函数使目录数量自加一 ptr = fullpath + strlen(fullpath);
*ptr++ = '/';
*ptr = ; if((dp = opendir(fullpath)) == NULL)
return(myfunc(fullpath,&statbuf,)); while((dirp = readdir(dp)) != NULL){
if(strcmp(dirp->d_name,".") == || strcmp(dirp->d_name,"..") == )
continue;
strcpy(ptr, dirp->d_name); dopath();
} closedir(dp); } void ftw(char *pathname)
{
#ifdef PATH_MAX
const int PATH_LEN = PATH_MAX;
#else
const int PATH_LEN = ;
#endif fullpath = malloc(PATH_LEN); strncpy(fullpath,pathname,PATH_LEN); fullpath[PATH_LEN - ] = '\0'; dopath();
} int main(int argc, char *argv[])
{
if(argc != ){
printf("enter two args\n");
exit();
} ftw(argv[]); printf("regular files: }\n",nreg);
printf("directories : }\n",ndir);
printf("block special: }\n",nblk);
printf("char special: }\n",nchr);
printf("FIFOS: }\n",nfifo);
printf("symboli links: }\n",nslink);
printf("sockets: }\n",nsock); }

  需要注意的是第60行的判断一定不能少,因为在linux下创建一个空目录的时候,用ls查看其属性发现里面大小是2.写一个程序看下一个空目录里都有神马?

 #include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
#include<string.h> int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
char pathname[]; if(argc != ){
printf("please enter to args\n");
exit();
} puts(argv[]); strncpy(pathname, argv[], ); if((dp = opendir(pathname)) == NULL){
printf("opendir %s error\n",argv[]);
exit();
} while((dirp = readdir(dp)) != NULL){
printf("%s\n",dirp->d_name);
} closedir(dp); return ;
}

  虽然是个空目录但是给的结果却是

.

..

  因此要忽略掉这两个,否则会进入死循环;

【C】——APUE小程序之递归遍历目录的更多相关文章

  1. (实用篇)PHP不用递归遍历目录下所有文件的代码

    <?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...

  2. Linux下文件及目录的一些操作(附递归遍历目录源码)

    1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...

  3. Java 之递归遍历目录

    Java 之递归遍历目录 一.内容 输出指定目录(文件夹)下的所有文件(包括目录)的绝对路径 二.源代码:RecursiveListDirectory.java package cn.com.zfc. ...

  4. Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。

    题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就 ...

  5. Python递归遍历目录下所有文件

    #自定义函数: import ospath="D:\\Temp_del\\a"def gci (path): """this is a stateme ...

  6. VC/MFC 下 递归遍历目录下的所有子目录及文件

    在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历.递归遍历代码如下: /******************* ...

  7. iKcamp出品|微信小程序|工具安装+目录说明|基于最新版1.0开发者工具初中级教程分享

    iKcamp官网:http://www.ikcamp.com 访问官网更快阅读全部免费分享课程:<iKcamp出品|全网最新|微信小程序|基于最新版1.0开发者工具之初中级培训教程分享>. ...

  8. IO流-递归遍历目录下指定后缀名结尾的文件名称

    /* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...

  9. 微信小程序第3课 目录结构及小知识点

    目录 目录结构 安装包下载地址 一.pages目录介绍 二.index目录介绍 index.js(相当JavaScript文件,必不可少的) index.json(可以不需要) index.wxml( ...

随机推荐

  1. C#基础第一天-作业答案

    题一答案: Console.WriteLine("请输入a"); int a = Convert.ToInt32(Console.ReadLine()); Console.Writ ...

  2. Processing支持中文显示

    Processing 默认不支持中文,中文显示成框框,我使用的版本是:2.2.1,进行如下设置,并且重启processing就可以支持中文了: 可以看到中文了:

  3. CSS边框闪烁呼吸样式

    <html> <body> <head> .arrow_box{animation: glow 800ms ease-out infinite alternate; ...

  4. JDK1.5新特性,基础类库篇,System类

    一. 背景 System.getenv(String)方法继续有效:增加了新的System.getenv()方法,返回保存环境变量的Map<String,String>. 同时增加了以纳秒 ...

  5. 奶瓶(beini)破解无线密码流程:安装、抓包、从虚拟机(VMware)拷贝握手包(拷贝到硬盘、U盘)、跑包

    1. 环境 1). Windows 7 64位版本 2). VMware 9.0.2版本 3). 奶瓶1.2.3版本(beini-1.2.3.iso) 2. 安装 2.1 安装方式一 将beini-1 ...

  6. 菜鸟学SSH(十)——Hibernate核心接口

    在使用Hibernate的时候,我们通常都会用的Configuration.SessionFactory.Session.Transaction.Query和Criteria等接口.通过这些接口可以, ...

  7. Function.apply()在提升程序性能方面的技巧

    我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 alert(Math.max(5,8))   //8alert(Math.max(5,7 ...

  8. [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage

    这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...

  9. [AWS vs Azure] 云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析

    云计算里AWS和Azure的探究(5) ——EC2和Azure VM磁盘性能分析 在虚拟机创建完成之后,CPU和内存的配置等等基本上是一目了然的.如果不考虑显卡性能,一台机器最重要的性能瓶颈就是硬盘. ...

  10. 每日英语:Nanjing's New Sifang Art Museum Illustrates China's Cultural Boom

    In a forest on the outskirts of this former Chinese capital, 58-year-old real-estate developer Lu Ju ...