linux的readdir和readdir_r函数

article/2025/10/9 2:20:57

1.首先要打开目录文件

DIR *opendir( const char *name);

DIR *fdopendir( int fd);

2.读取目录文件信息的函数    

注意:这是个库函数

struct dirent *readdir( DIR *dirp);

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

文件目录结构体:

struct dirent {ino_t          d_ino;       /* inode number 索引节点号*/off_t          d_off;       /* not an offset; see NOTES 在目录文件中的偏移*/unsigned short d_reclen;    /* length of this record 文件名长*/unsigned char  d_type;   /*type of file; not supported by all  filesystem types 文件类型*/              char           d_name[256]; /* filename 文件名,最长255字符*/};

 

d_type的值为:

DT_BLK This is a block device.

DT_CHR This is a character device.

DT_DIR This is a directory.

DT_FIFO This is a named pipe (FIFO).

DT_LNK This is a symbolic link.

DT_REG This is a regular file.

DT_SOCK This is a UNIX domain socket.

DT_UNKNOWN The file type is unknown.

readdir()函数实例:

注意:

每次使用readdir后,readdir会读到下一个文件,readdir是依次读出目录中的所有文件,每次只能读一个

这个特性和readdir_r()一样

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char **argv){DIR *pDir = NULL;struct dirent * pEnt = NULL;unsigned int cnt = 0;	if (argc != 2){printf("usage: %s dirname\n", argv[0]);return -1;}pDir = opendir(argv[1]);if (NULL == pDir){perror("opendir");return -1;}	while (1){pEnt = readdir(pDir);if(pEnt != NULL){if (pEnt->d_type == DT_REG){printf("是普通文件:");}else{printf("不是普通文件:");}printf("name:[%s]	\n", pEnt->d_name);cnt++;}else{break;}};printf("总文件数为:%d\n", cnt);return 0;
}

结果:

$ ./a.out .
是普通文件:name:[a.c]	
不是普通文件:name:[.]	
不是普通文件:name:[..]	
是普通文件:name:[a.out]	
不是普通文件:name:[12_sr]	
不是普通文件:name:[10_sr]	
不是普通文件:name:[17_sr]	
不是普通文件:name:[15_sr]	
不是普通文件:name:[14.sr]	
不是普通文件:name:[18_sr]	
不是普通文件:name:[udp]	
不是普通文件:name:[16_sr]	
不是普通文件:name:[tcp]	
总文件数为:13

readdir_r():

注意:

这三个参数

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc, char **argv)
{DIR *pDir = NULL;struct dirent * pEnt = NULL;struct dirent *entry = (struct dirent *)malloc(sizeof(struct dirent));struct dirent **result = (struct dirent **)malloc(sizeof(struct dirent));unsigned int cnt = 0;unsigned int ret = 0;	if (argc != 2){printf("usage: %s dirname\n", argv[0]);return -1;}pDir = opendir(argv[1]);if (NULL == pDir){perror("opendir");return -1;}ret = readdir_r(pDir , entry , result);printf("return	:%d	\n", ret);printf("name	:[%s]	\n", entry->d_name);printf("name	:[%s]	\n", result[0]->d_name);ret = readdir_r(pDir , entry , result);printf("return	:%d	\n", ret);printf("name	:[%s]	\n", entry->d_name);printf("name	:[%s]	\n", result[0]->d_name);return 0;}

结果:

 

 

 

 

 


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

相关文章

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…

Ubuntu远程桌面连接

0.相关准备&#xff1a; 开启屏幕共享功能的NX、显卡诱骗器、远程连接电脑&#xff08;Ubuntu&#xff09; 屏幕共享开启如下&#xff1a; 在设置的sharing中打开Remote Login&#xff0c;选中Screen Sharing 选中对应的选项&#xff0c;如图所示&#xff0c;完成在对应网络…

Ubuntu开启图形化远程桌面

1、先更新一下软件列表。 sudo apt-get update sudo apt-get upgrade 2、有些闪退情况就是因为缺少安装gnome这一步&#xff0c;用以下命令安装一下。 sudo apt install gnome-session gdm3 3、 在右上角点击设置。 4、选择“共享”标签页&#xff0c;点击右上角的滑块开启共…