linux opendir和readdir的使用

2023-05-16


1    opendir

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

DIR *opendir(const char *name);


传入name路径,成功则返回非空DIR指针,否则返回NULL


2    readdir


     #include <dirent.h>


       struct dirent *readdir(DIR *dirp);


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


readdir一般要配合opendir使用,readdir不是线程安全函数,代替他的有readdir_r。

readdir返回 struct dirent *指针,读完目录下所有文件时,返回NULL


如果系统支持readdir_r,建议用readdir_r , readdir_r成功返回0。


关于struct dirent结构体:

 On Linux, the dirent structure is defined as follows:


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


3    closedir

      #include <sys/types.h>


       #include <dirent.h>


       int closedir(DIR *dirp);

closedir配合opendir使用。



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>	
#include <dirent.h>
#include <errno.h>
//DIR *opendir(const char *name);


#ifndef  LOG_TRACE
#define LOG_TRACE printf
#define LOG_INFO(msg)  \
	do{ \
		LOG_TRACE msg; \
		LOG_TRACE("[%s %d] \n",__FUNCTION__,__LINE__);\
	}while(0)

#define LOG_ERROR(err_info)  \
	do{ \
		LOG_TRACE err_info; \
		LOG_TRACE("[%s %d]  \n",__FUNCTION__,__LINE__);\
	}while(0)
#endif

extern int errno;
int open_dir_1(const char *pDirname)
{

	DIR * dirp = NULL; 
	struct dirent * pDirent= NULL;
	if((NULL == pDirname) ||
		(0 == strlen(pDirname)))
	{
		LOG_ERROR(("param error"));
		return -1;
	}
	
	dirp = opendir(pDirname);
	if (NULL == dirp)
	{
		LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
		return -1;
	}
	
	while (NULL != (pDirent = readdir(dirp)))
	{
	
		if (pDirent->d_type == DT_DIR )
		{
			LOG_ERROR(("dir      [%s]       ",pDirent->d_name));
		}
		else if(pDirent->d_type == DT_REG)
		{
			LOG_ERROR(("file     [%s]        ",pDirent->d_name));
		}

	}

	closedir(dirp);
	return 0;
}

int open_dir_2(const char *pDirname)

{

	DIR * dirp = NULL; 
	struct dirent * pDirent= NULL;
	struct dirent *pStResult = NULL;
	
	if((NULL == pDirname) ||
		(0 == strlen(pDirname)))
	{
		LOG_ERROR(("param error"));
		return -1;
	}
	dirp = opendir(pDirname);
	if (NULL == dirp)
	{
		LOG_ERROR(("opendir %s failed! error_no: %s",pDirname , strerror(errno)));
		return -1;
	}

	pDirent = (struct dirent *)malloc(sizeof(struct dirent));
	if(!pDirent)
	{
		LOG_ERROR(("pDirent error"));
		closedir(dirp);
		return -1;
	}
	while (( 0== readdir_r(dirp,pDirent,&pStResult))&&
		(pStResult != NULL))
	{
		if (pDirent->d_type == DT_DIR )
		{
			LOG_ERROR(("dir      [%s]       ",pDirent->d_name));
		}
		else if(pDirent->d_type == DT_REG)
		{
			LOG_ERROR(("file     [%s]        ",pDirent->d_name));
		}

	}

	closedir(dirp);
	
	return 0;
}


int main()
{
	LOG_ERROR(("***********"));
	open_dir_1("/share/");
	LOG_ERROR(("------------"));
	open_dir_2("/share/");

	LOG_ERROR(("-++--+++--"));
	return 0;
}


运行结果:


./a.out [19@gcc test_opendir_readdir.c[C
root@ubuntu:/share# 
root@ubuntu:/share# 
root@ubuntu:/share# gcc test_opendir_readdir.c 
/a.out 
***********[main 113]  
file     [zlib-1.2.8.tar.gz]        [open_dir_1 54]  
file     [1.tmp]        [open_dir_1 54]  
file     [log.c]        [open_dir_1 54]  
file     [list.c]        [open_dir_1 54]  
dir      [11]       [open_dir_1 50]  
file     [test.out]        [open_dir_1 54]  
file     [test_system_func.c]        [open_dir_1 54]  
file     [a.out]        [open_dir_1 54]  
file     [test_strncpy.c]        [open_dir_1 54]  
file     [test_proc_partitions.c]        [open_dir_1 54]  
file     [test.c]        [open_dir_1 54]  
file     [New0001.c]        [open_dir_1 54]  
dir      [curl-7.51.0]       [open_dir_1 50]  
file     [test_ftok.c]        [open_dir_1 54]  
file     [log.h]        [open_dir_1 54]  
file     [test_gettimeofday.c]        [open_dir_1 54]  
file     [csdn.c]        [open_dir_1 54]  
file     [client.c]        [open_dir_1 54]  
file     [1.txt]        [open_dir_1 54]  
file     [test_opendir_readdir.c]        [open_dir_1 54]  
file     [curl-7.51.0.tar.gz]        [open_dir_1 54]  
file     [2.txt]        [open_dir_1 54]  
file     [opendir.c]        [open_dir_1 54]  
dir      [zlib-1.2.8]       [open_dir_1 50]  
file     [123.rmvb]        [open_dir_1 54]  
dir      [ffmpeg_learn]       [open_dir_1 50]  
file     [test_sem.c]        [open_dir_1 54]  
file     [test_list.c]        [open_dir_1 54]  
file     [list.h]        [open_dir_1 54]  
file     [server]        [open_dir_1 54]  
dir      [ProFFmpeg]       [open_dir_1 50]  
dir      [ffmpeg-3.1.6]       [open_dir_1 50]  
dir      [yasm-1.3.0]       [open_dir_1 50]  
file     [New0003.c]        [open_dir_1 54]  
dir      [.]       [open_dir_1 50]  
dir      [..]       [open_dir_1 50]  
file     [ffmpeg-3.1.6.tar.gz]        [open_dir_1 54]  
file     [types.h]        [open_dir_1 54]  
file     [New0002.c]        [open_dir_1 54]  
file     [simple.out]        [open_dir_1 54]  
file     [server.c]        [open_dir_1 54]  
dir      [learn]       [open_dir_1 50]  
file     [simple_ffmpeg_player.c]        [open_dir_1 54]  
file     [client]        [open_dir_1 54]  
dir      [abc]       [open_dir_1 50]  
file     [yasm-1.3.0.tar.gz]        [open_dir_1 54]  
file     [output.yuv]        [open_dir_1 54]  
file     [mySDLFirst.out]        [open_dir_1 54]  
------------[main 115]  
file     [zlib-1.2.8.tar.gz]        [open_dir_2 100]  
file     [1.tmp]        [open_dir_2 100]  
file     [log.c]        [open_dir_2 100]  
file     [list.c]        [open_dir_2 100]  
dir      [11]       [open_dir_2 96]  
file     [test.out]        [open_dir_2 100]  
file     [test_system_func.c]        [open_dir_2 100]  
file     [a.out]        [open_dir_2 100]  
file     [test_strncpy.c]        [open_dir_2 100]  
file     [test_proc_partitions.c]        [open_dir_2 100]  
file     [test.c]        [open_dir_2 100]  
file     [New0001.c]        [open_dir_2 100]  
dir      [curl-7.51.0]       [open_dir_2 96]  
file     [test_ftok.c]        [open_dir_2 100]  
file     [log.h]        [open_dir_2 100]  
file     [test_gettimeofday.c]        [open_dir_2 100]  
file     [csdn.c]        [open_dir_2 100]  
file     [client.c]        [open_dir_2 100]  
file     [1.txt]        [open_dir_2 100]  
file     [test_opendir_readdir.c]        [open_dir_2 100]  
file     [curl-7.51.0.tar.gz]        [open_dir_2 100]  
file     [2.txt]        [open_dir_2 100]  
file     [opendir.c]        [open_dir_2 100]  
dir      [zlib-1.2.8]       [open_dir_2 96]  
file     [123.rmvb]        [open_dir_2 100]  
dir      [ffmpeg_learn]       [open_dir_2 96]  
file     [test_sem.c]        [open_dir_2 100]  
file     [test_list.c]        [open_dir_2 100]  
file     [list.h]        [open_dir_2 100]  
file     [server]        [open_dir_2 100]  
dir      [ProFFmpeg]       [open_dir_2 96]  
dir      [ffmpeg-3.1.6]       [open_dir_2 96]  
dir      [yasm-1.3.0]       [open_dir_2 96]  
file     [New0003.c]        [open_dir_2 100]  
dir      [.]       [open_dir_2 96]  
dir      [..]       [open_dir_2 96]  
file     [ffmpeg-3.1.6.tar.gz]        [open_dir_2 100]  
file     [types.h]        [open_dir_2 100]  
file     [New0002.c]        [open_dir_2 100]  
file     [simple.out]        [open_dir_2 100]  
file     [server.c]        [open_dir_2 100]  
dir      [learn]       [open_dir_2 96]  
file     [simple_ffmpeg_player.c]        [open_dir_2 100]  
file     [client]        [open_dir_2 100]  
dir      [abc]       [open_dir_2 96]  
file     [yasm-1.3.0.tar.gz]        [open_dir_2 100]  
file     [output.yuv]        [open_dir_2 100]  
file     [mySDLFirst.out]        [open_dir_2 100]  
-++--+++--[main 118]  
root@ubuntu:/share# 


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

linux opendir和readdir的使用 的相关文章

  • 段错误...关于你好世界

    这段代码非常简单 但我在 x86 64 Linux 系统上遇到了段错误 这让我很烦恼 刚开始接触asm 请耐心等待 与 NASM 组装nasm f elf64 test asm 与连接ld o test test o SECTION tex
  • Apache 端口转发 80 到 8080 并访问 Apache (80) 中托管的应用程序,即 phpMyadmin 和 Tomcat (8080)

    我想访问托管在 tomcat 服务器 8080 中的应用程序 myapp 当前可以通过以下方式访问http example com 8080 myapp http example com 8080 myapp in http example
  • 选择fasta文件中氨基酸超过300个且“C”出现至少4次的序列

    我有一个包含蛋白质序列的 fasta 文件 我想选择超过 300 个氨基酸且半胱氨酸 C 氨基酸出现超过 4 次的序列 我使用此命令来选择具有超过 300 个 aa 的序列 cat 72hDOWN fasta fasta bioawk c
  • 任何退出 bash 脚本但不退出终端的方法

    当我使用exitshell 脚本中的命令 该脚本将终止终端 提示符 有什么方法可以终止脚本然后停留在终端中吗 我的剧本run sh预计通过直接获取或从另一个脚本获取来执行 编辑 更具体地说 有两个脚本run2 sh as run sh ec
  • 使用 shell 脚本发送 HTML 邮件

    如何使用 shell 脚本发送 HTML 电子邮件 首先 您需要撰写消息 最低限度由这两个标头组成 MIME Version 1 0 Content Type text html 以及适当的消息正文 p Hello world p 获得后
  • 使用脚本检查 git 分支是否领先于另一个分支

    I have branch1 and branch2我想要某种 git branch1 isahead branch2 这将显示如果branch1已承诺branch2没有 也可能指定这些提交 我无法检查差异原因branch2 is在之前br
  • 使用 ioctl 在 C++ 中以编程方式添加路由

    我编写了简单的 C 函数 添加了新路线 void addRoute int fd socket PF INET SOCK DGRAM IPPROTO IP struct rtentry route memset route 0 sizeof
  • bash 将输出重定向到文件,但结果不完整

    重定向命令输出的问题已经被问过很多次了 但是我有一个奇怪的行为 我使用的是 bash shell debian 版本 4 3 30 1 release 并尝试将输出重定向到文件 但并非所有内容都记录在文件中 我尝试运行的 bin 文件是 l
  • 如何在 Linux 和 C 中使用文件作为互斥体?

    我有不同的进程同时访问 Linux 中的命名管道 并且我想让此访问互斥 我知道可以使用放置在共享内存区域中的互斥体来实现这一点 但作为一种家庭作业 我有一些限制 于是 我想到的是对文件使用锁定原语来实现互斥 我做了一些尝试 但无法使其发挥作
  • BASH:输入期间按 Ctrl+C 会中断当前终端

    我的 Bash 版本是 GNU bash version 4 3 11 1 release x86 64 pc linux gnu 我有一段这样的代码 while true do echo n Set password read s pas
  • Linux无法删除文件

    当我找到文件时 我在删除它们时遇到问题 任务 必须找到带有空格的文件并将其删除 我的尝试 rm find L root grep i 但我有错误 rm cannot remove root test No such file or dire
  • 在汇编中使用 printf 会导致管道传输时输出为空,但可以在终端上使用

    无输出 https stackoverflow com questions 54507957 printf call from assembly do not print to stdout即使在终端上 当输出不包含换行符时也有相同的原因
  • 为什么 fork 炸弹没有使 android 崩溃?

    这是最简单的叉子炸弹 我在许多 Linux 发行版上执行了它 但它们都崩溃了 但是当我在 android 终端中执行此操作时 即使授予后也没有效果超级用户权限 有什么解释为什么它没有使 Android 系统崩溃吗 一句话 ulimit Li
  • 如何才能将 TCP 连接返回到同一端口?

    机器是 RHEL 5 3 内核 2 6 18 有时我在 netstat 中注意到我的应用程序有连接 建立了 TCP 连接本地地址 and 国外地址是一样的 其他人也报告了同样的问题 症状与链接中描述的相同 客户端连接到本地运行的服务器的端口
  • 仅使用containerd(不使用Docker)修剪容器镜像

    如果我刚刚containerd安装在 Linux 系统上 即 Docker 是not安装 如何删除未使用的容器映像以节省磁盘空间 Docker 就是这么方便docker system prune https docs docker com
  • 相当于Linux中的导入库

    在 Windows C 中 当您想要链接 DLL 时 您必须提供导入库 但是在 GNU 构建系统中 当您想要链接 so 文件 相当于 dll 时 您就不需要链接 为什么是这样 是否有等效的 Windows 导入库 注意 我不会谈论在 Win
  • FileOutputStream.close() 中的设备 ioctl 不合适

    我有一些代码可以使用以下命令将一些首选项保存到文件中FileOutputStream 这是我已经写了一千遍的标准代码 FileOutputStream out new FileOutputStream file try BufferedOu
  • ALSA:snd_pcm_writei 调用时缓冲区不足

    当运行我最近从灰烬中带回来的旧程序时 我遇到了缓冲区不足的情况 该程序将原始声音文件完全加载到内存中 2100 字节长 525 帧 并准备 ALSA 进行输出 44 1khz 2 通道 有符号 16 位 if err snd pcm set
  • 为什么 Linux 没有 DirectX API?

    在考虑现代显卡的 Windows 系统上 DirectX API 的驱动程序端实现时 我想知道为什么此实现在非 Windows 系统 尤其是 Linux 上不可用 由于明显缺乏此功能 我只能假设有一个我无视的充分理由 但在我的原始理解中 我
  • 适用于 Linux 的轻量级 IDE [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi

随机推荐

  • Pixhawk源码笔记三:串行接口UART和Console

    这里 xff0c 我们对 APM UART Console 接口进行讲解 如有问题 xff0c 可以交流30175224 64 qq com 新浪 64 WalkAnt xff0c 转载本博客文章 xff0c 请注明出处 xff0c 以便更
  • C/C++中二维数组和指针关系分析

    在C c 43 43 中 xff0c 数组和指针有着密切的关系 xff0c 有很多地方说数组就是指针式错误的一种说法 这两者是不同的数据结构 其实 xff0c 在C c 43 43 中没有所谓的二维数组 xff0c 书面表达就是数组的数组
  • 四叉树空间索引原理及其实现

    今天依然在放假中 xff0c 在此将以前在学校写的四叉树的东西拿出来和大家分享 四叉树索引的基本思想是将地理空间递归划分为不同层次的树结构 它将已知范围的空间等分成四个相等的子空间 xff0c 如此递归下去 xff0c 直至树的层次达到一定
  • DirectXShaderCompiler mac编译

    Directxshader compiler mac编译 1 前置条件 Please make sure you have the following resources before building GitPython Version
  • intel -tbb 源码cmake构建

    cmake minimum required VERSION 3 0 0 FATAL ERROR set CMAKE CXX STANDARD 17 project tbb CXX add library tbb SHARED void c
  • 如何修改数据库密码

    多可文档管理系统是自带数据库的 xff0c 就是你在安装多可文档管理系统的同时 xff0c 数据库就已经自动安装上了 这个数据库有个默认密码 xff0c 为了数据库里的数据安全 xff0c 建议你安装完多可后 xff0c 就立刻修改数据库的
  • iOS编译openmp

    1 下载openmp源码 https github com llvm llvm project releases download llvmorg 14 0 6 openmp 14 0 6 src tar xz 2 下载ios toolch
  • 我的2013-从GIS学生到GIS职业人的飞跃

    我的 2013 从 GIS 学生GIS 职业人的飞跃 前言 xff1a 从末日中度过了 2012 年 xff0c 我们伟大的人类把这个世界末日的谎言给揭穿了 xff0c 但是不知不觉中 xff0c 2013 年又即将悄悄从我们身边溜走 xf
  • 矩阵的特征值和特征向量的雅克比算法C/C++实现

    矩阵的特征值和特征向量是线性代数以及矩阵论中非常重要的一个概念 在遥感领域也是经常用到 xff0c 比如多光谱以及高光谱图像的主成分分析要求解波段间协方差矩阵或者相关系数矩阵的特征值和特征向量 根据普通线性代数中的概念 xff0c 特征值和
  • windows多线程详解

    在一个牛人的博客上看到了这篇文章 xff0c 所以就转过来了 xff0c 地址是http blog csdn net morewindows article details 7421759 本文将带领你与多线程作第一次亲密接触 xff0c
  • tiff文件读取

    以下是VC下读取TIFF文件的代码 char szFileName 61 34 K 地图 fujian DEM fujian1 tif 34 TIFF tiff 61 TIFFOpen szFileName 34 r 34 打开Tiff文件
  • GIS开发人员需要掌握的知识和技能

    对于GIS行业 xff0c 可能很多人不是很了解 xff0c 对我来说也不是很了解 xff0c 在此呢 xff0c 我就我自己的看法发表一下简单的看法 xff0c 有什么不同的意见可以一起交流 GIS虽说是属于地理科学或者说测绘科学与技术的
  • GIS算法的一点理解

    在GIS这个专业也混了好几年了 xff0c 但是始终没有对GIS算法有过真正的研究 xff0c 可以说大部分不懂 目前关于GIS算法的书籍不是特别多 xff0c 数来数去也就那么几本 xff0c 南师大几个老师编写的地理信息系统算法基础 x
  • char*转LPCWSTR解决方案

    在Windows编程中 xff0c 经常会碰到字符串之间的转换 xff0c char 转LPCWSTR也是其中一个比较常见的转换 下面就列出几种比较常用的转换方法 1 通过MultiByteToWideChar函数转换 MultiByteT
  • 一阶互补滤波,二阶互补滤波,卡尔曼滤波

    一阶互补 a 61 tau tau 43 loop time newAngle 61 angle measured with atan2 using the accelerometer 加速度传感器输出值 newRate 61 angle
  • 项目实用makefile

    在上一篇文章 小项目实用makefile 中 xff0c 已经说明了单个makefile管理层次目录的局限性 本文 xff0c 主要总结一下项目中的一种实用makefile树写法 xff0c 为10来个人协作的中小型项目makefile编写
  • MPU6050工作原理及STM32控制MPU6050

    一 简介 1 要想知道MPU6050工作原理 xff0c 得先了解下面俩个传感器 xff1a 陀螺仪传感器 xff1a 陀螺仪的原理就是 xff0c 一个旋转物体的 旋转轴所指的方向在不受外力影响时 xff0c 是不会改变的 人们根据这个道
  • 自动驾驶(四十九)---------Kavser二次开发

    我们知道CAN总线是连接车身各个模块之间的桥梁 xff0c 通过协议通讯 xff0c 在车辆标定和测试中很多情况是用上位机和车身相连 xff0c 收发满足CAN总线的信号 这中间如何通讯呢 xff1f 这就需要用到Kavser Kvaser
  • Linux嵌入式设备时钟同步到硬件

    时间修改命令 date s 34 2022 06 27 11 51 02 34 同步到硬件 hwclock w 显示硬件时钟 hwclock r
  • linux opendir和readdir的使用

    1 opendir include lt sys types h gt include lt dirent h gt DIR opendir const char name 传入name路径 xff0c 成功则返回非空DIR指针 xff0c