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

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

opendir()函数:
头文件:
 

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

函数原型:
 

Dir* opendir(const char* pathname);

函数功能:
获取pathname目录下的所有文件和目录的列表,如果pathname是个文件或失败则返回NULL,并设置errno
返回值DIR结构体的原型为:struct _dirstream
typedef struct _dirstream DIR;
 

struct _dirstream
{void* _fd;char* _data;int _entry_data;char* _ptr;int _entry_ptr;size_t _allocation;size_t _size;_libc_lock_define (,_lock)
};

readdir()函数:
头文件:#include <dirent.h>
函数原型:
 

struct dirent *readdir(DIR *dir_handle);

函数功能:读取opendir返回的那个列表
 

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字符 */};

其中d_type文件类型又有如下情况:
 

enum
{DT_UNKNOWN = 0,         //未知类型DT_FIFO = 1,            //管道DT_CHR = 2,             //字符设备DT_DIR = 4,             //目录DT_BLK = 6,             //块设备DT_REG = 8,             //常规文件DT_LNK = 10,            //符号链接DT_SOCK = 12,           //套接字DT_WHT = 14             //链接
};

演示示例:
 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>// main 函数的 argv[1] char * 作为 所需要遍历的路径 传参数给 listDir
void listDir(char *path)
{   DIR *pDir;//定义一个 DIR 类的指针struct dirent *ent;//定义一个结构体 dirent 的指针,dirent 结构体见上int i = 0;    char childpath[512];//定义一个字符数组,用来存放读取的路径pDir = opendir(path); //opendir 方法打开 path 目录,并将地址付给 pDir 指针memset(childpath, 0, sizeof(childpath)); //将字符数组 childpath 的数组元素全部置零//读取 pDir 打开的目录,并赋值给 ent, 同时判断是否目录为空,不为空则执行循环体while ((ent = readdir(pDir)) != NULL){//读取 打开目录的文件类型 并与 DT_DIR 进行位与运算操作,即如果读取的 d_type 类型为                     //DT_DIR (=4 表示读取的为目录)if (ent->d_type & DT_DIR) {if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {//如果读取的 d_name 为 . 或者.. 表示读取的是当前目录符和上一目录符, 用 //contiue 跳过,不进行下面的输出continue;}//如果非. ..则将 路径 和 文件名 d_name 付给 childpath, 并在下一行 prinf 输出sprintf(childpath, "%s/%s", path, ent->d_name);printf("path:%s\n", childpath);//递归读取下层的字目录内容, 因为是递归,所以从外往里逐次输出所有目录(路径+目录名)//然后才在 else 中由内往外逐次输出所有文件名listDir(childpath);}//如果读取的 d_type 类型不是 DT_DIR, 即读取的不是目录,而是文件,则直接输出 d_name, 即输出文件名elseprintf("%s\n", ent->d_name);}
}int main(int argc, char *argv[])
{listDir(argv[1]); //第一个参数为想要遍历的linux目录。例如,当前目录为 ./ ,上一层目录为../return 0;
}

 


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

相关文章

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&…

Windows10远程登陆Ubuntu桌面

简介 我们在使用Ubuntu系统有时可能需要看Ubuntu上的仿真界面和可视化数据&#xff0c;可能会有这样一个需求&#xff1a;使用Windows系统接入Ubuntu&#xff0c;本文章提供一种方法&#xff0c;使用Windows自带的远程桌面Ubuntu安装VNC解决问题。 Ubuntu上的配置 1.下载配置…

window10远程桌面控制Ubuntu系统

Windows操作系统作为全球使用最多的个人操作系统&#xff0c;在我们身边随处可见&#xff0c;但放眼各类电子设备的操作系统&#xff0c;windows并不是一家独大&#xff0c;服务器系统大多基于Linux系统开发、手机操作系统几乎都是安卓、更不用说还有苹果的iOS、树莓派、Ubuntu…

Ubuntu Desktop 启用远程桌面(Vino和TigerVNC方式)

文章目录 前言使用Vino方式无显示器使用使用TigerVNC方式 前言 在很多领域的生产开发工作中常常需要用到 Ubuntu Desktop 系统&#xff0c;但是在一些日常的工作交流中又离不开Windows系统&#xff0c;这种时候比较常用的解决方案就是在Windows系统上使用虚拟机安装Ubuntu。不…

Ubuntu 22.04 远程桌面

参考&#xff1a;1、Ubuntu 22.04 Finally Supports Remote Desktop Control via MS RDP Protocol | UbuntuHandbook 2、22.04 - Remote Desktop Sharing authentication password changes every reboot - Ask Ubuntu 一、无法连接 有可能是没登录到 gnome 桌面。因为 gnome r…