日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2024/9/27 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 10Linux服务器编程之:opendir()函数,readdir()函数,rewinddir()函数,telldir()函数和seekdir()函数,closedir()函数 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1 opendir所需的頭文件

#include<sys/types.h>

#include<dirent.h>

2函數(shù)聲明

DIR *opendir(const char *name);

DIR *fdopendir(int fd);

通過(guò)opendir來(lái)打開(kāi)一個(gè)文件夾

3readdir依賴的頭文件

#include<dirent.h>

4函數(shù)聲明

struct dirent *readdir(DIR *dirp);

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

函數(shù)說(shuō)明

通過(guò)這兩個(gè)函數(shù)實(shí)現(xiàn)讀取目錄

關(guān)于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每次返回一條記錄項(xiàng),DIR*指針指向下一條記錄項(xiàng)

6.rewinddir

#include <sys/types.h>

#include <dirent.h>

void rewinddir(DIR *dirp);

把目錄指針恢復(fù)到目錄的起始位置。

7.telldir/seekdir

#include <dirent.h>

long telldir(DIR *dirp);

#include <dirent.h>

void seekdir(DIR *dirp, long offset);

?

函數(shù)描述

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);

函數(shù)描述

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.通過(guò)遞歸的方式遍歷目錄

#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;

}

這個(gè)程序還是不如ls -R健壯,它有可能死循環(huán),思考一下什么情況會(huì)導(dǎo)致死循

環(huán)。

總結(jié)

以上是生活随笔為你收集整理的10Linux服务器编程之:opendir()函数,readdir()函数,rewinddir()函数,telldir()函数和seekdir()函数,closedir()函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。