linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)
头文件:
#include<dirent.h>
#include<sys/types.h> opendir():
函数原型:
DIR * opendir(const char* path);
打开一个目录,在失败的时候返回NULL(如果path对应的是文件,则返回NULL)
DIR 结构体的原型为:struct_dirstream
在linux系统中:
typedef struct __dirstream DIR;
struct __dirstream
{
void *__fd; /* `struct hurd_fd' pointer for descriptor. */
char *__data; /* Directory block. */
int __entry_data; /* Entry number `__data' corresponds to. */
char *__ptr; /* Current pointer into the block. */
int __entry_ptr; /* Entry number `__ptr' corresponds to. */
size_t __allocation; /* Space allocated for the block. */
size_t __size; /* Total valid data in the block. */
__libc_lock_define (, __lock) /* Mutex lock for this structure. */
}; readdir():
函数原型:
struct dirent * readdir(DIR * dir_handle);
本函数读取dir_handle目录下的目录项,如果有未读取的目录项,返回目录项,否则返回NULL。
循环读取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+]; /* file name (null-terminated) 文件名,最长255字符 */
} closedir():
函数原型: int closedir(DIR * dir_handle);
程序如下:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h> #include <sys/stat.h>
#include <dirent.h>
#include <errno.h> //搜索 指定目录下的所有文件及其子目录下的文件 void getFileName(char * dirPath)
{
DIR *dir=opendir(dirPath);
if(dir==NULL)
{
printf("%s\n",strerror(errno));
return;
}
chdir(dirPath);//进入到当前读取目录
struct dirent *ent;
while((ent=readdir(dir))!=NULL)
{
if(strcmp(ent->d_name,".")==||strcmp(ent->d_name,"..")==)
{
continue;
}
struct stat st;
stat(ent->d_name,&st);
if(S_ISDIR(st.st_mode))
{
getFileName(ent->d_name);
}
else
{
printf("%s\n",ent->d_name);
}
}
closedir(dir);
chdir("..");//返回当前目录的上一级目录
} int main(int argc, char *argv[])
{
getFileName("/home/gexin/program/");
return ;
}
linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)的更多相关文章
- ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解
open(打开文件) 相关函数 read,write,fcntl,close,link,stat,umask,unlink,fopen 表头文件 #include<sys/types.h> ...
- Linux下C语言编程实现spwd函数
Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...
- Linux基础与Linux下C语言编程基础
Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...
- 笔记整理——Linux下C语言正则表达式
Linux下C语言正则表达式使用详解 - Google Chrome (2013/5/2 16:40:37) Linux下C语言正则表达式使用详解 2012年6月6日Neal627 views发表评论 ...
- LINUX下C语言编程调用函数、链接头文件以及库文件
LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...
- linux 下程序员专用搜索源码用来替代grep的软件ack(后来发现一个更快的: ag), 且有vim插件的
发现一个比ack更快更好用的: https://github.com/ggreer/the_silver_searcher , 使用时命令为ag,它是基于ack的代码二次开发的,所有使用方法基本 ...
- Linux下C语言编程基础学习记录
VIM的基本使用 LINUX下C语言编程 用gcc命令编译运行C语言文件 预处理阶段:将*.c文件转化为*.i预处理过的C程序. 编译阶段:将*.i文件编译为汇编代码*.s文件. 汇编阶段:将*.s ...
- 【转】Linux基础与Linux下C语言编程基础
原文:https://www.cnblogs.com/huyufeng/p/4841232.html ------------------------------------------------- ...
- Linux下C语言编程中库的使用
零.问题 1. 为什么要用到库? 2. 我要用一个库,但是,尼玛命令行上该怎么写呢?或者说库文件如何使用? 3. Linux的库在那些地方? 4. 什么是静态库,什么是动态库,二者有啥区别? 5. 常 ...
随机推荐
- 处理xml c#
using System.Xml; using System; using System.Text; using System.Net; using System.Collections; using ...
- 百度前端技术学院2015JavaScript基础部分-BOM
5.1 任务描述 实现以下函数 // 判断是否为IE浏览器,返回-1或者版本号 function isIE() { // your implement } // 设置cookie function s ...
- Dojo的Gridx使用jsonrest需要注意的地方
在使用gridx时,如果要使用jsonrest,主要的工作主要是在服务端,服务端在返回数据时,必须在返回头里添加Content-Range: 0-9/73 属性和值,其中0表示从第0条记录开始,9表示 ...
- asp.net生成随机密码
public string RandCode(int n) { char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j' ...
- 【visio 2007操作】
1.visio改变画布大小 两种方法:1)按住ctrl,可以鼠标拉动调整背景绘图大小2)点击菜单栏“文件”-“页面尺寸”,选择“调整大小以适应绘图内容”并点击确定
- Australian troops to the fight against Islamic State militants.
He arrived in Arnhem Land on Sunday, honouring an election promise to spend a week every year in an ...
- 关于JavaScript和html的随笔
最近听了一些关于JavaScript和html的讲课和读了一些书籍.因为我是给项目做网站知道的,所以要特别的注意和努力.JavaScript是一门挺好用的脚本语言,比较简单灵活,在这上面我深有体会,因 ...
- MySQL数据库9 - 日期与时间函数
一 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为数 ...
- sql 日期格式输出 - 转
SELECT CONVERT(varchar(100), GETDATE(), 0) 05 9 2011 9:12AM SELECT CONVERT(varchar(100), GETDATE(), ...
- difference between forward and sendredirect
Difference between SendRedirect and forward is one of classical interview questions asked during jav ...