一、整体操作

①打开目录文件:opendir

②读取目录项:readdir

③关闭目录文件:closedir

注意:所需要用到的头文件

#ifndef __HEAD_H__ //防止头文件被重复定义
#define __HEAD_H__//防止头文件被重复定义#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>#endif//防止头文件被重复定义

二、opendir/closedir

  1. opendir

①函数原型:DIR *opendir(const char *name);

②功能:打开目录获得一个目录流指针

③函数参数:name:目录文件的路径

④返回值:成功返回目录流指针,失败返回NULL

#include  "head.h"int main(int argc, char const *argv[])
{DIR *dp = NULL;dp = opendir(".");//打开'.'if (NULL == dp){perror("fail to opendir");return -1;}
}

2.closedir

①函数原型:int closedir(DIR *dirp);

②功能:关闭目录流

③函数参数:dirp:目录流指针

④返回值:成功返回0 ,失败返回-1

#include "head.h"int main(int argc, char const *argv[])
{DIR *dp = NULL;struct dirent *pp = NULL;dp = opendir(".");if (NULL == dp){perror("fail to opendir");return -1;}while (1){pp = readdir(dp);if (NULL == pp){break;}if ('.' == pp->d_name[0]){continue;}printf("%s\n", pp->d_name);}closedir(dp); //关闭目录流return 0;
}

三、readdir

①函数原型:struct dirent *readdir(DIR *dirp);

②功能:读取目录中下一个目录项的信息

③函数参数:dirp:目录流指针

④返回值:成功返回目录项结构体信息,失败返回NULL

#include "head.h"int main(int argc, char const *argv[])
{DIR *dp = NULL;struct dirent *pp = NULL;dp = opendir(".");if (NULL == dp){perror("fail to opendir");return -1;}while (1){pp = readdir(dp); //读取目录中下一个目录项的信息if (NULL == pp){break;}if ('.' == pp->d_name[0]) //不输出隐藏文件{continue;}printf("%s\n", pp->d_name);}closedir(dp);return 0;
}

输出结果:

四、mkdir

①函数原型:int mkdir(const char *pathname, mode_t mode);

②功能:创建一个目录文件

③函数参数:pathname:目录的路径 mode:目录的权限

④返回值:成功返回0 失败返回-1

#include "head.h"int main(int argc, char const *argv[])
{mkdir("aaa",0664);//创建一个aaa的文件,基于权限为0664return 0;
}

创建结果:

五、rmdir

①函数原型:int rmdir(const char *pathname);

②功能:删除空目录

③函数参数:pathname:删除目录路径

④返回值:成功返回0 ,失败返回-1

#include "head.h"int main(int argc, char const *argv[])
{rmdir("aaa"); //将上一步mkdir所创建的aaa文件删除return 0;
}

六、getcwd

①函数原型:char *getcwd(char *buf, size_t size);

②功能:获得当前工作目录的绝对路径

③函数参数:buf:存放路径字符串空间首地址 size:最多存放字符串的个数

④返回值:成功返回字符串空间首地址,失败返回NULL

#include "head.h"int main(int argc, char const *argv[])
{char tmp[4096] = {0};getcwd(tmp, sizeof(tmp));//获得当前工作目录的绝对路径,存放到tmp当中printf("%s\n",tmp);return 0;
}

输出结果:

七、chdir

①函数原型:int chdir(const char *path);

②功能:切换当前的工作路径

③函数参数:path:要切换到的路径

④返回值:成功返回0 ,失败返回-1

#include "head.h"int main(int argc, char const *argv[])
{char tmp[4096] = {0};getcwd(tmp, sizeof(tmp));//获得当前工作目录的绝对路径,存放到tmp当中printf("%s\n",tmp);//输出当前绝对路径printf("================================\n");chdir("..");//返回上一级目录getcwd(tmp, sizeof(tmp));//获得上一级工作目录的绝对路径,存放到tmp当中printf("%s\n",tmp);//输出上一级工作目录的绝对路径return 0;
}

输出结果:

(5)Linux基础——opendir/closedir 、readdir、mkdir 、rmdir、getcwd、chdir详细含义用法及介绍(基础)相关推荐

  1. Linux库函数之opendir/closedir/readdir

    在Linux环境下,有时候需要编写一些实用的工具,比如检索功能,最近在做病毒查杀应用开发,涉及到批量扫描指定目录下文件, 因为要测试大量病毒文件,该部分功能又是要通过API集成到其他应用软件中,设计时 ...

  2. linux下opendir函数,linux 下 opendir 和 readdir 函数的应用

    #include #include #include #include #include #include void printall(const char *path, int max){ DIR ...

  3. opendir、readdir和closedir函数

    注意:在Linux中,目录的输入格式:/mnt//fghs./mnt/fghs./mnt/fghs和/mnt/fghs//是等效的,都一样. #include <sys/types.h> ...

  4. 打开、读取以及关闭目录[ opendir()、 readdir()和 closedir() ]

    文章目录 一.打开目录 opendir 二.读取目录 readdir rewinddir 函数 三.关闭目录 closedir 函数 示例代码 打开.读取.关闭一个普通文件可以使用 open().re ...

  5. 硬链接,软链接,link,rename,symlink,opendir和readdir

    什么是硬链接 struct stat {nlink_t st_nlink; /* Number of hard links}; stat结构体就有一个成员变量----硬链接数 使用ln命令就可以创建硬 ...

  6. 【CentOS Linux 7】实验1【VMware安装、新建虚拟机;63个基础命令运行结果图】

    Linux系统及应用---调研报告 [CentOS Linux 7]实验1[VMware安装.新建虚拟机:63个基础命令运行结果图] [CentOS Linux 7]实验2[Shell编程及应用] [ ...

  7. php dir opendir,php中目录操作opendir()、readdir()及scandir()用法示例

    本文实例讲述了php中目录操作opendir().readdir()及scandir()用法.分享给大家供大家参考,具体如下: opendir(path,context)若成功,则该函数返回一个目录流 ...

  8. Linux 基础介绍-基础命令

    文章目录 01 学习目标 02 Linux/Unix 操作系统简介 2.1 Linux 操作系统的目标 2.2 Linux 操作系统的作用 2.3 Unix 家族历史 2.4 Linux 家族历史 2 ...

  9. linux进程隐藏 hook readdir函数 挂载覆盖/proc/pid 目录

    前言 上篇介绍了如何在有源码的情况下,通过 argv[] 及 prctl 对进程名及参数进行修改,整篇围绕/proc/pid/目录和 ps.top 命令进行分析,做到了初步隐藏,即修改了 /proc/ ...

最新文章

  1. C++实现九九乘法表
  2. 不从事编程、学python有用吗-没想到,学会Python即使不做程序员都能月入过万!...
  3. python 画图 内存-用python 10min手写一个简易的实时内存监控系统
  4. Spring IoC 源码系列(一)BeanDefinition 初始化与注册
  5. android+建模工具,什么是适用于Android Studio的3D模型环境的最佳工具
  6. ASP.NET MVC3中的ViewBag动态性
  7. Nginx配置文件详细说明(转)
  8. 去中心化借贷协议24小时清算超1300万美元
  9. nagios监控mysql服务_nagios监控mysql服务
  10. Kotlin基础-对象声明和表达式
  11. C#:获取web.config中配置的IP地址
  12. 微信浏览器下载APK文件的实现方案
  13. 带你走进CoDeSys
  14. iShot--免费Mac截图工具
  15. Puppet自动化Nginx+Mongrel负载均衡配置
  16. 使用POI导出Excel使单元格内容换行
  17. 磁盘坏道的检测及修复
  18. 猪头三生活平凡的一天
  19. MYSQL数据库 增删改查基础语句
  20. 无人机学习笔记之电池篇

热门文章

  1. 悟空CRM在保险行业的应用
  2. 克转换成千克怎么算python_磅到公斤和克的转换python函数
  3. Sharding-jdbc连接kingbasev8r6跨表分页
  4. 跟着老陈学嵌入式-C语言入门之类Linux编译环境搭建
  5. Metasploit(二)
  6. 表现层(UI)、业务逻辑层(BLL)、数据访问层(DAL)
  7. 【linux deploy】安卓手机安装linux(ubuntu) 提供各种工具安装链接
  8. Ubuntu 输入法fcitx方块乱码解决设置
  9. html a标签触发不了onclick()事件
  10. python:mplfinance 画K线图