【C】——APUE小程序之递归遍历目录
递归降序遍历目录层次结构,并按文件类型计数。
先介绍相关的函数:
#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小程序之递归遍历目录的更多相关文章
- (实用篇)PHP不用递归遍历目录下所有文件的代码
<?php /** * PHP 非递归实现查询该目录下所有文件 * @param unknown $dir * @return multitype:|multitype:string */ fu ...
- Linux下文件及目录的一些操作(附递归遍历目录源码)
1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...
- Java 之递归遍历目录
Java 之递归遍历目录 一.内容 输出指定目录(文件夹)下的所有文件(包括目录)的绝对路径 二.源代码:RecursiveListDirectory.java package cn.com.zfc. ...
- Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。
题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就 ...
- Python递归遍历目录下所有文件
#自定义函数: import ospath="D:\\Temp_del\\a"def gci (path): """this is a stateme ...
- VC/MFC 下 递归遍历目录下的所有子目录及文件
在MFC下要实现文件夹的递归遍历,可用CFileFind类,依次读取文件夹下的子文件夹和文件,并判断通过判断是文件夹还是文件来决定递归遍历.递归遍历代码如下: /******************* ...
- iKcamp出品|微信小程序|工具安装+目录说明|基于最新版1.0开发者工具初中级教程分享
iKcamp官网:http://www.ikcamp.com 访问官网更快阅读全部免费分享课程:<iKcamp出品|全网最新|微信小程序|基于最新版1.0开发者工具之初中级培训教程分享>. ...
- IO流-递归遍历目录下指定后缀名结尾的文件名称
/* *自定义遍历目录下指定后缀名结尾文件的名称的方法: * * param file:指定目录 name:指定后缀名 */ 1 public static void FileName(File fi ...
- 微信小程序第3课 目录结构及小知识点
目录 目录结构 安装包下载地址 一.pages目录介绍 二.index目录介绍 index.js(相当JavaScript文件,必不可少的) index.json(可以不需要) index.wxml( ...
随机推荐
- python标准库介绍——35 pipes 模块详解
==pipes 模块== (只用于 Unix) ``pipes`` 模块提供了 "转换管道 (conversion pipelines)" 的支持. 你可以创建包含许多外部工具调用 ...
- django -- 多对多关系的实现
在django中表和表之间的多对多关系有两种实现方案: 方案一:直接使用django自动实现的多对多关系. 方案二:自己写连接表.然而告诉django在实现多对多关系时要使用的连接表. 一.方案一: ...
- IOS团队开发之——CocoaPods 第三方库管理工具
使用前需要下载ruby 的gem 命令镜像,mac 下自带有.但一般不用,直接访问国外网站有限制. 下面安装 http://ruby.taobao.org/ http://blog.devtang.c ...
- Kinect v2 记录
最多可同时识别跟踪 6 人,每人可识别到 25 个关节数据.可以根据上身 10 个关节数据来判断坐姿状态. 物理极限识别范围:0.5m – 4.5m,最佳识别范围:0.8m – 3.5m. 深度数据可 ...
- eclipse 运行 emulator时,PANIC:Could not open emulator 的解决办法
使用eclipse启动emulator的时候,出现PANIC:Could not open emulator,模拟器无法正常的运行. 经过搜索得知,因为我的SDK的环境变量出问题,需要重新配置下环境变 ...
- git从ssh到提交到github
1.安装 Git 客户端 yum install git 2.打开 Git Bash,开始键入用户信息,和github通讯用的,不能乱写: git config --global user.name ...
- 一条SQL语句中算日销售额和月销售额
刚刚做项目的时候用到的 用户表:用户ID,用户名,余额 流水表:时间,用户ID,用户名,类型(0充值,1消费),变更金额 现在要查每个用户的日销售额和月销售额,本来最简单的方法是先把所有用户查出来,然 ...
- Atitit atiplat_reader 基于url阅读器的新特性
Atitit atiplat_reader 基于url阅读器的新特性 1.1. feature功能特性1 1.2. note1 1.1. feature功能特性 支持url数据源,实际就是只支持一层连 ...
- 【Unity】第12章 导航网格和寻路
开发环境:Win10.Unity5.3.4.C#.VS2015 创建日期:2016-05-09 一.简介 NavMesh(导航网格)是3D游戏世界中用于实现"动态"物体自动寻路的一 ...
- tar.xz文件格式的压缩与解压
从网上下载了一个man的安装文件,格式为tar.xz,默认下载到当前目录下 //下载man源码并以原文件名保存,如果要指定保存的文件名用小写-o name指定 curl -O https://www. ...