C语言遍历文件目录:readdir,opendir

article/2025/10/9 2:19:31

环境:Linux系统

头文件:

#include<sys/types.h>
#include<dirent.h>

一、opendir

原型

DIR* opendir (const char * path );

参数与功能

path为目录路径,打开一个目录,在失败的时候返回一个空的指针。

返回值

DIR*,DIR结构体类似于FILE,是一个内部结构,以下几个函数用这个内部结构保存当前正在被读取的目录的有关信息,其内容如下:

struct __dirstream   
{   void *__fd;    char *__data;    int __entry_data;    char *__ptr;    int __entry_ptr;    size_t __allocation;    size_t __size;    __libc_lock_define (, __lock)    
};   
typedef struct __dirstream DIR;  

通过opendir函数返回的dir结构可被以下函数使用:

struct dirent *readdir(DIR *dp);    
void rewinddir(DIR *dp);   
int closedir(DIR *dp);    
long telldir(DIR *dp);   
void seekdir(DIR *dp,long loc);  

二、readdir

原型

struct dirent* readdir(DIR* dir_handle);

参数与功能

DIR* dir_handle就是opendir返回的DIR结构,readdir的功能就是读一个目录。

返回值

返回是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+1]; /* file name (null-terminated) 文件名,最长255字符 */  
}  

从上述定义也能够看出来,dirent结构体存储的关于文件的信息很少,所以dirent同样也是起着一个索引的作用,如果想获得类似ls -l那种效果的文件信息,必须要靠stat函数了。

三、stat

原型

int stat(const char *file_name, struct stat *buf);

参数与功能

file_name为readdir返回的dirent结构体中的d_name,buf为stat结构buf。

通过readdir函数读取到的文件名存储在结构体dirent的d_name成员中,而函数stat();的作用就是获取文件名为d_name的文件的详细信息,存储在stat结构体中。以下为stat结构体的定义:

struct stat {   mode_t     st_mode;       //文件访问权限   ino_t      st_ino;       //索引节点号   dev_t      st_dev;        //文件使用的设备号   dev_t      st_rdev;       //设备文件的设备号   nlink_t    st_nlink;      //文件的硬连接数   uid_t      st_uid;        //所有者用户识别号   gid_t      st_gid;        //组识别号   off_t      st_size;       //以字节为单位的文件容量   time_t     st_atime;      //最后一次访问该文件的时间   time_t     st_mtime;      //最后一次修改该文件的时间   time_t     st_ctime;      //最后一次改变该文件状态的时间   blksize_t st_blksize;    //包含该文件的磁盘块的大小   blkcnt_t   st_blocks;     //该文件所占的磁盘块   
};  

四、示例

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>/* 文件大小和修改时间 */
static int get_file_size_time(const char *filename)
{struct stat statbuf;/* 判断未打开文件 */ if(stat(filename,&statbuf)==-1){printf("Get stat on %s Error: %s\n", filename, strerror(errno));return(-1);}if(S_ISDIR(statbuf.st_mode)) // 目录return(1);if(S_ISREG(statbuf.st_mode)) // 文件printf("%s size: %ld bytes\tmodified at %s", filename, statbuf.st_size, ctime(&statbuf.st_mtime));return(0);
}int main(int argc,char **argv)
{DIR *dirp;struct dirent *direntp;int stats;if(argc!=2){printf("Usage: %s filename\n\a", argv[0]);exit(1);}if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1)) // 文件或出现错误exit(1);/* 打开目录 */	if((dirp=opendir(argv[1]))==NULL){printf("Open Directory %s Error: %s\n", argv[1], strerror(errno));exit(1);}/* 返回目录中文件大小和修改时间 */while((direntp=readdir(dirp))!=NULL) {/* 给文件或目录名添加路径:argv[1]+"/"+direntp->d_name */char dirbuf[512]; memset(dirbuf,0,sizeof(dirbuf)); strcpy(dirbuf,argv[1]); strcat(dirbuf,"/"); strcat(dirbuf,direntp->d_name); if(get_file_size_time(dirbuf)==-1) break;}closedir(dirp);exit(1);
}

运行结果:

五、总结

遍历一个文件夹中内容的布奏:

首先,使用opendir函数打开目录a,返回指向目录a的DIR结构体c。

接着,我们调用readdir( c)函数读取目录a下所有文件(包括目录),返回指向目录a下所有文件的dirent结构体d。

然后,遍历d,调用stat(d->name,stat *e)来获取每个文件的详细信息,存储在stat结构体e中。

 

参考:

https://blog.csdn.net/u012349696/article/details/50083787

https://blog.csdn.net/hb707934728/article/details/52100853


http://chatgpt.dhexx.cn/article/qjSJ7uaP.shtml

相关文章

嵌入式学习之Linux系统编程---9 目录IO之readdir函数

1、readdir函数的函数原型 #include <dirent.h> struct dirent *readdir(DIR *dirp);对于readdir函数来说&#xff0c;它只有目录流指针这一个参数&#xff0c;这个目录流指针就是使用opendir这个函数的返回值。 该函数在man手册的第三页&#xff0c;该函数如果执行成功…

readdir函数解析

函数原型: struct dirent *readdir(DIR *dirp); 首先纠正一个很多人都错误理解的事实,readdir不是系统调用,它是glibc的封装函数,而且readdir系统调用是存在的,原型如下: int readdir(unsigend int fd, struct old_linux_dirent *dirp, unsigned int count); glibc的readdi…

C/C++的readdir和readdir_r函数(遍历目录)

1.首先要打开目录文件 DIR *opendir( const char *name); DIR *fdopendir( int fd); 2.读取目录文件信息的函数 注意&#xff1a;这是个库函数 struct dirent *readdir( DIR *dirp); int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **res…

Linux下 C 遍历目录(opendir,readdir函数)

opendir()函数: 头文件&#xff1a; #include <sys/types.h> #include <dirent.h> 函数原型&#xff1a; Dir* opendir(const char* pathname); 函数功能&#xff1a; 获取pathname目录下的所有文件和目录的列表&#xff0c;如果pathname是个文件或失败则返…

linux——读取文件(read)

ssize_t read(int fd, void *buf, size_t count); 将fd中的内容读取到buf中。 代码&#xff1a; #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #inc…

linux的readdir和readdir_r函数

1.首先要打开目录文件 DIR *opendir( const char *name); DIR *fdopendir( int fd); 2.读取目录文件信息的函数 注意&#xff1a;这是个库函数 struct dirent *readdir( DIR *dirp); int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **res…

readdir函数

readdir会不断读取目中的文件及目录&#xff0c;但不会读子目录中的文件。 #include <sys/types.h> #include <dirent.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> int main() {DIR *dirp opendir("/home/python/Des…

readdir不保证读取的文件顺序

readdir用于读取某个文件夹中的全部文件或文件夹&#xff0c;相当于ls。 但是readdir并不保证读取后的文件顺序&#xff0c;在不同的操作系统上可能有不同的顺序。 在某些场景下需要注意&#xff0c;比如读取配置文件时&#xff0c;可能会根据配置文件进行一些初始化&#xf…

Ubuntu 18.04安装远程桌面

Ubuntu 18.04安装远程桌面 陈拓 2021/08/05-2020/08/08 1. Putty登录 IP地址 192.168.0.103 登录账户 ccdc xxxxxxxx 2. Ubuntu 18.04安装桌面 如果安装的系统已经带桌面跳过这一步。 2.1 查看linux系统版本 lsb_release -a 2.2 安装桌面 sudo apt-get install ubun…

ubuntu远程桌面连接windows

ubuntu远程桌面连接windows 1&#xff1a;使用ubuntu自带软件remmina 2&#xff1a;打开该软件后&#xff0c;点击长方形 3&#xff1a; 服务器&#xff1a;windows电脑内网ip 用户名&#xff1a;电脑用户名 密码&#xff1a;电脑用户名的登陆密码 点击右下角&#xff1a;保存…

Ubuntu 远程桌面的方式

提示&#xff1a;仅仅是按照记忆所写的笔记&#xff0c;如果你看到这篇笔记&#xff0c;按照操作出了问题&#xff0c;评论就好了&#xff0c;我会完善一下。笔记内容以外的问题不要评论&#xff0c;我不管。 vino & dconf-editor 该方式适用于ubuntu desktop 18.04 及以后…

Ubuntu18.04远程桌面连接

一、安装 xrdp、tightvncserver sudo apt-get install tightvncserver xrdp二、安装xubuntu-desktop sudo apt-get install xubuntu-desktop三、修改配置文件 echo xfce4-session >~/.xsession sudo vi /etc/xrdp/startwm.sh在下图位置加入"xfce4-session" 在…

多ubuntu主机远程桌面连接方案

一、需求背景 公司有一批ubuntu的主机&#xff0c;需要研发远程上去进行代码调试&#xff0c;普通的远程桌面方式不易于管理&#xff0c;并且无法进行连接控制。 二、方案制定 基于web的远程方案有Guacamole、NoVNC两种方案&#xff0c;但都不利于后期工具与公司整体的SSO进行对…

Ubuntu Server 18.04安装远程桌面并连接

文章目录 一、安装桌面环境Xfce二、安装 Xrdp三、设置root密码四、连接Xrdp服务器五、设置终端 尝试了很多种方法&#xff0c;折腾了一晚上终于搞出来了呜呜…顺便记录一下,以免下次忘记! 一、安装桌面环境Xfce Ubuntu 服务器通常使用命令行进行管理&#xff0c;并且默认没有安…

Windows10远程桌面Ubuntu16.04

自己的笔记本配置太低&#xff0c;有很多图形界面的软件&#xff0c;需要在服务器上运行&#xff0c;通常只用SSH方式访问的命令行方式是无法实现的。 虽然配置XShell XManager可以实现打开图形程序&#xff0c;但速度之慢&#xff0c;即使内网也无法忍受。 今天来推荐一个更…

Ubuntu 安装远程桌面

转自&#xff1a;https://blog.csdn.net/heyangyi_19940703/article/details/77994416 1.安装xrdp软件: 运行Terminal,执行以下命令&#xff1a; sudo apt-get -y install xfce4 xrdp vnc4server 2.安装完成&#xff0c;查看下相关软件包 执行命令&#xff1a; dpkg -L xrdp…

Ubuntu 系统下如何远程访问 Windows 桌面 ?

你一定听说过 Windows 应用程序远程桌面连接。该应用程序系统自带不用安装&#xff0c;并允许您远程访问另一台 PC 或服务器。它使用远程桌面协议建立远程桌面连接会话。 一些 Linux 发行版可能会提供 RDP 客户端来连接到 Windows 系统。但是&#xff0c;对于某些 linux 发行版…

ubuntu20.04远程桌面这样装

我的记录本 安装了新系统之后&#xff0c;直接就进行了远程桌面的测试1.系统设置2.安装xrdp3.安装dconf-editor并设置4.切换windows系统打开远程桌面 安装了新系统之后&#xff0c;直接就进行了远程桌面的测试 更新源都没有动&#xff0c;直接进行远程桌面的测试。 参考文献&a…

Windows 远程桌面 Ubuntu

参考 Windows远程桌面工具连接Ubuntu系统使用总结_CHH3213的博客-CSDN博客_远程连接ubuntu 开启ssh服务&#xff08;非必须 查看ssh是否已经开启 sudo ps -e | grep ssh 如果最后返回是sshd&#xff0c;证明ssh已经开启&#xff0c;跳到第四步 第二步&#xff0c;如果没有…

ubuntu使用VNC实现远程桌面

2022.11更新 目前我已经放弃VNC了&#xff0c;虽然局域网连着画面还行&#xff0c;但是太容易出bug了。 现在更推荐向日葵或Todesk这些远程控制软件&#xff0c;虽然画面糊一点&#xff0c;但是稳定&#xff0c;而且不受局域网限制。 前言 我是在树莓派4B上安装的Ubuntu20.10&…