码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-09-16 00:24:29      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   os   ar   文件   div   sp   cti   



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()函数

标签:des   style   io   os   ar   文件   div   sp   cti   

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/39301421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!