1 opendir所需的头文件

#include<sys/types.h>

#include<dirent.h>

2函数声明

DIR *opendir(const char *name);

DIR *fdopendir(int fd);

通过opendir来打开一个文件夹

3readdir依赖的头文件

#include<dirent.h>

4函数声明

struct dirent *readdir(DIR *dirp);

int readdir_r(DIR *dirp, struct dirent*entry, struct dirent **result);

函数说明

通过这两个函数实现读取目录

关于struct dirent,定义如下:

struct dirent {

ino_t  d_ino;             /* inode number */

off_t  d_off;             /* not an offset; see NOTES */

unsignedshort d_reclen;    /* length of this record*/

unsigned char d_type;      /* typeof file; not supported

by all filesystem types */

char d_name[256];        /* filename */

};

readdir每次返回一条记录项,DIR*指针指向下一条记录项

6.rewinddir

#include <sys/types.h>

#include <dirent.h>

void rewinddir(DIR *dirp);

把目录指针恢复到目录的起始位置。

7.telldir/seekdir

#include <dirent.h>

long telldir(DIR *dirp);

#include <dirent.h>

void seekdir(DIR *dirp, long offset);

函数描述

telldir - return current location indirectory stream

The  telldir() function returns the current location associated with

the directory stream dirp.

8.closedir

#include<sys/types.h>

#include<dirent.h>

int closedir(DIR*dirp);

函数描述

The  closedir() function closes the directory stream associated with

dirp. A successful call to closedir() also  closes  the underlying

file descriptor associated with dirp. The directory stream descrip‐

tor dirp is not available after thiscall.

9.通过递归的方式遍历目录

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

#include<dirent.h>

#include<stdio.h>

#include<string.h>

#define MAX_PATH1024

/* dirwalk: applyfcn to all files in dir */

void dirwalk(char*dir, void (*fcn)(char *))

{

char name[MAX_PATH];

struct dirent *dp;

DIR *dfd;

if ((dfd = opendir(dir)) == NULL) {

fprintf(stderr, "dirwalk: can't open%s\n", dir);

return;

}

while ((dp = readdir(dfd)) != NULL) {

if (strcmp(dp->d_name, ".") ==0

|| strcmp(dp->d_name, "..") ==0)

continue; /* skip self and parent */

if (strlen(dir)+strlen(dp->d_name)+2> sizeof(name))

fprintf(stderr, "dirwalk: name %s %stoo long\n",

dir, dp->d_name);

else {

sprintf(name, "%s/%s", dir,dp->d_name);

(*fcn)(name);

}

}

closedir(dfd);

}

/* fsize: printthe size and name of file "name" */

void fsize(char*name)

{

struct stat stbuf;

if (stat(name, &stbuf) == -1) {

fprintf(stderr, "fsize: can't access%s\n", name);

return;

}

if ((stbuf.st_mode & S_IFMT) ==S_IFDIR)

dirwalk(name, fsize);

printf("%8ld %s\n",stbuf.st_size, name);

}

int main(intargc, char **argv)

{

if (argc == 1) /* default: currentdirectory */

fsize(".");

else

while (--argc > 0)

fsize(*++argv);

return 0;

}

这个程序还是不如ls -R健壮,它有可能死循环,思考一下什么情况会导致死循

环。

10Linux服务器编程之:opendir()函数,readdir()函数,rewinddir()函数,telldir()函数和seekdir()函数,closedir()函数相关推荐

  1. linux读取文件修改时间函数,Linux服务器编程之utime()函数修改文件存取时间

    Linux服务器编程之utime()函数修改文件存取时间 C语言utime()函数:修改文件的存取时间和更改时间 头文件: #include #include 定义函数: int utime(cons ...

  2. 8Linux服务器编程之:chdir()函数和cd命令,getcwd()函数和pwd

     1chdir依赖的头文件 #include<unistd.h> 2函数定义 int chdir(const char *path); int fchdir(int fd); 函数说明 ...

  3. 11Linux服务器编程之:VFS虚拟文件系统,dup()函数和dup2()函数

     1dup函数和dup2函数 #include<unistd.h> int dup(intoldfd); int dup2(intoldfd, int newfd); dup和dup2 ...

  4. 9Linux服务器编程之:mkdir()函数和rmdir()函数

     1 mkdir依赖的头文件 #include<sys/stat.h> #include<sys/types.h> 2.函数声明: int mkdir(const char ...

  5. Linux服务器编程之:link()函数,ln命令,symlink,readlink,案例说明

    1 link()依赖头文件 #include<unistd.h> 2函数定义 int link(const char *oldpath,const char *newpath); 函数说明 ...

  6. Linux服务器编程之:truncate()函数+案例说明

    1.依赖头文件 #include<unistd.h> #include<sys/types.h> 2.函数定义: int truncate(const char *path,o ...

  7. Linux服务器编程之:utime()函数

    1.依赖的头文件 #include<sys/types.h> #include<utime.h> 2函数声明 int utime(const char *filename, c ...

  8. Linux服务器编程之:chown()函数,chown命令

    1.依赖的头文件 #include<unistd.h> 2.函数定义: //通过传入path的方式,改变和文件的拥有关系,如果他是一个符号链接,则跟踪它 int chown(const c ...

  9. linux chown 函数用噶,Linux服务器编程之:chown()函数,chown命令

    1.依赖的头文件 #include 2.函数定义: //通过传入path的方式,改变和文件的拥有关系,如果他是一个符号链接,则跟踪它 int chown(const char *path, uid_t ...

最新文章

  1. linux下用js生成xml,js2xml:将javascript字符串转换为xml
  2. shell编程入门步步高(八、函数)
  3. html网页语言是什么,HTML是什么?
  4. 科大星云诗社动态20210211
  5. java object转泛型_为什么Java的泛型要用擦除实现
  6. RIP实验总结之一被动接口和单播更新
  7. 图数据库 HugeGraph : IndexLabel
  8. 极客大学产品经理训练营 产品思维和产品意识 作业2
  9. 达梦之路——基于Linux平台(redhat)安装部署DM7单库
  10. 光敏传感器实验报告_光敏电阻传感器实验报告
  11. SQL中Group by 简单理解
  12. 人事管理系统实现(一)
  13. 第一章 绪论 思维导图
  14. 解决edge浏览器无法打开pdf文件问题
  15. rap2搭建,mysql,redis,nginx安装,node环境安装,rap2安装
  16. java弹力球游戏_小班弹力球游戏教案
  17. Android 常用开源总结
  18. ASEMI代理AD8061ARTZ-REEL7原装ADI车规级AD8061ARTZ-REEL7
  19. Springboot----项目整合微信支付(处理微信支付回调通知)
  20. React input 获得焦点/失去焦点判断

热门文章

  1. VTK:参数样条用法实战
  2. boost::mpi模块指针序列化测试
  3. boost::mp11::mp_replace_front相关用法的测试程序
  4. boost::hana::value_or用法的测试程序
  5. boost::math::tools::continued_fraction_b用法的测试程序
  6. boost::intrusive::circular_slist_algorithms用法的测试程序
  7. GDCM:iU22原始数据提取器的测试程序
  8. VTK:可视化算法之Office
  9. VTK:图片之Cast
  10. OpenCV通过填充修复损坏的图像的实例(附完整代码)