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

简介:  1 opendir所需的头文件 #include<sys/types.h> #include<dirent.h> 2函数声明 DIR *opendir(const char *name); DIR *fdopendir(int fd); 通过opendir来打开一个文件夹 3readdir依赖的头文件 #i


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健壮,它有可能死循环,思考一下什么情况会导致死循

环。

目录
相关文章
|
9天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
10天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
14天前
|
Linux
linux下搭建tftp服务器教程
在Linux中搭建TFTP服务器,需安装`tftp-server`(如`tftpd-hpa`)。步骤包括:更新软件包列表,安装`tftpd-hpa`,启动并设置开机自启,配置服务器(编辑`/etc/default/tftpd-hpa`),添加选项,然后重启服务。完成后,可用`tftp`命令进行文件传输。例如,从IP`192.168.1.100`下载`file.txt`: ``` tftp 192.168.1.100 &lt;&lt;EOF binary put file.txt quit EOF ```
28 4
|
17天前
|
Linux
关于Linux目录访问函数总结
关于Linux目录访问函数总结
13 1
|
13天前
|
网络协议 Python
pythonTCP客户端编程连接服务器
【4月更文挑战第6天】本教程介绍了TCP客户端如何连接服务器,包括指定服务器IP和端口、发送连接请求、处理异常、进行数据传输及关闭连接。在Python中,使用`socket`模块创建Socket对象,然后通过`connect()`方法尝试连接服务器 `(server_ip, server_port)`。成功连接后,利用`send()`和`recv()`进行数据交互,记得在通信完成后调用`close()`关闭连接,确保资源释放和程序稳定性。
|
6天前
|
Linux 数据安全/隐私保护
Linux基础与服务器架构综合小实践
【4月更文挑战第9天】Linux基础与服务器架构综合小实践
1192 6
|
12天前
|
Python
Python网络编程基础(Socket编程)UDP服务器编程
【4月更文挑战第8天】Python UDP服务器编程使用socket库创建UDP套接字,绑定到特定地址(如localhost:8000),通过`recvfrom`接收客户端数据报,显示数据长度、地址和内容。无连接的UDP协议使得服务器无法主动发送数据,通常需应用层实现请求-响应机制。当完成时,用`close`关闭套接字。
|
12天前
|
Linux 开发者
Linux文件编程(open read write close函数)
通过这些函数,开发者可以在Linux环境下进行文件的读取、写入和管理。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
84 4
|
17天前
|
传感器 Linux API
嵌入式Linux串口编程简介
嵌入式Linux串口编程简介
15 1
|
18天前
|
Linux 测试技术 C语言
【Linux】应用编程之C语言文件操作
【Linux】应用编程之C语言文件操作