Linux 目录流管理
1. 打开/关闭文件
1). 打开目录 / opendir
作用:
打开一个目录,将其与目录流联系起来,方便后续的操作头文件:
#include <dirent.h>
函数原型:
DIR *opendir(const char *name)
参数:
name: 欲打开的目录名(可包含路径)
DIR为一个结构体:typedef struct __dirstream DIR
对用户透明,无需关系其内容
返回值:
成功:返回目录流
失败:返回NULL
错误码:见errno相似函数:
FILE *fdopendir(int fd, const char *mode) //通过目录描述符打开文件
2). 关闭文件 / fclose
作用:
关闭一个已经打开的目录头文件:
#include <dirent.h>
函数原型:
int closedir(DIR *dirp)
参数:
- dirp: 打开目录时候返回的流指针(DIR *)
- 返回值:
成功:0
失败:-1
2. 读/写目录流
1). 目录流-读 / readdir & readdir_r
①. readdir
作用:
读取目录流信息头文件:
#include <dirent.h>
函数原型:
struct dirent *readdir(DIR *dirp)
参数:
dirp: 目录流
struct dirent:
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 file system types */
char d_name[256]; /* filename */
};
d_type的宏定义:
Macro Description 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 If the file type could not be determined, the value DT_UNKNOWN is returned in d_type.
- 返回值:
成功:dirent结构体指针
失败:NULL
②. readdir_r
作用:
读取目录流信息(多线程安全)头文件:
#include <dirent.h>
函数原型:
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
参数:
- dirp: 打开的目录流
- entry:
- result: 读取到的信息存储到该缓存中返回
- 返回值:
成功:0
失败:-1
3. 目录流定位
1). telldir
作用:
获取当前的读写位置头文件:
#include <dirent.h>
函数原型:
long telldir(DIR *dirp)
参数:
dir: 目录流
- 返回值:
成功:当前指针的位置
失败:-1
2). seekdir
作用:
修改当前的读写位置头文件:
#include <dirent.h>
函数原型:
void seekdir(DIR *dirp, long loc)
参数:
- dirp: 目录流
- loc: 从telldir()获取的目录流位置
- 返回值:
无返回值
3). rewinddir
作用:
将目录流指针指向开始位置头文件:
#include <dirent.h>
函数原型:
void rewinddir(DIR *dirp)
参数:
dirp: 目录流
- 返回值:
无返回值
4. 添加/删除目录
1). mkdir
作用:
创建一个新的目录头文件:
#include <sys/stat.h>
函数原型:
int mkdir(const char *pathname, mode_t mode)
参数:
pathname: 创建的目录名
mode: 创建目录的访问权限
mode = mode & ~umask
- 返回值:
成功:0
失败:-1
2). mkdir
作用:
删除一个目录头文件:
#include <unistd.h>
函数原型:
int rmdir(const char *pathname)
参数:
- pathname: 删除的目录名
- 返回值:
成功:0
失败:-1
5. 工作路径
1). 获取当前工作路径 / getcwd
作用:
获取当前工作路径头文件:
#include <unistd.h>
函数原型:
char *getcwd(char *buf, size_t size)
char *getwd(char *buf)
char *get_current_dir_name(void)
参数:
- buf: 暂存现工作路径
- size: buf的大小
- 返回值:
成功:当前工作路径
失败:NULL
2). 修改当前工作路径 / chdir
作用:
修改当前工作路径头文件:
#include <unistd.h>
函数原型:
int chdir(const char *path)
int fchdir(int fd)
参数:
- path: 目标工作路径
- fd: dir描述符
- 返回值:
成功:0
失败:-1
Linux 目录流管理的更多相关文章
- Linux目录权限管理
Linux目录权限管理 实验目标: 通过本实验掌握centos7/rhel7目录权限的管理.包括配置目录的所属组.SGID.读/写/执行权限等. 实验步骤: 1.创建目录/home/instruc ...
- Linux 文件流管理
1. 打开/关闭文件 1). 打开文件 / fopen 作用: 打开一个文件,将其与文件流联系起来,方便后续的操作 头文件: #include <stdio.h> 函数原型: FILE * ...
- Linux档案与目录的管理
本篇随笔中,主要介绍在Linux环境下,与档案和目录的管理相关的一些命令使用,具体包括如下几个方面: 目录的相关操作:cd,pwd,mkdir,rmdir(rm) 档案与目录的查视:ls 复制.删除与 ...
- Linux系统文件与目录权限管理
Linux文件目录权限管理 一.Linux文件属性及权限 1.Linux文件及目录权限及属性说明 (1)权限及属性说明 (2)文件权限说明 三种权限说明:r 读 read w 写 write x ...
- Linux下用户管理、目录结构
linux的文件系统是采用层级式的树状目录结构,在此结构中的最上层是根目录“/”,然后在此目录下再创建其他的目录. 目录介绍: / root,存放root用户的相关文件 home,存放普通用户的相关文 ...
- Linux目录和文件——管理目录和文件的命令
Linux目录和文件——管理目录和文件的命令 摘要:本文主要学习了Linux系统中关于目录和文件的操作. cd命令 cd命令用来切换工作目录,是Change Directory的缩写. 基本语法 cd ...
- linux档案和目录的管理
资料来自鸟哥的linux私房菜,记录下来供自己平常使用参考 一:目录和路径: cd:change direcoty,变换目录的意思,就是从一个目录变到另一个目录,然后可以用绝对路径去变换目录,也可以用 ...
- Linux目录机构及目录管理
Linux的目录结构与目录管理 Linux目录结构: 1 目录创建规则 FHS 文件系统层次化标准 指定Linux操作系统哪些目录必须具备 /boot /bin /sbin /etc /sys /pr ...
- linux 目录结构+常用命令+压缩命令+vim使用+及基础知识
linux目录架构 / 根目录 /bin 常用的命令 binary file 的目录 /boot 存放系统启动时必须读取的档案,包括核心 (kernel) 在内 /boot/grub/menu.lst ...
随机推荐
- Jersey Client传递中文参数
客户端需要客户端的包: <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jerse ...
- [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]
Virus We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...
- Alwayson--工作流程
Alwayson的工作流程: 1. 在主副本上,用户提交数据修改事务,等待服务器返回成功表示: 2. 在主副本上,将事务日志固化(harden),SQL SERVER调用Logwriter线程将事务日 ...
- RabbitMQ 消息队列 DEMO
1. 引用 RabbitMQ.Client.5.1.0 2. http://localhost:15672/ public class TestController : ApiController { ...
- (C#)字符串反转
方法一: public static string Reverse(string name) { if (String.IsNullOrEmpty(name)) { ...
- CefSharp禁止弹出新窗体,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接
1.实现ILifeSpanHandler接口,代码如下: using CefSharp; using CefSharp.WinForms; using System; using System.Col ...
- Python常用第三方模块(长期更新)
1.keyboard #监控键盘 2.PIL#处理图片 3.operator #操作列表 4.shelve #数据存储方案 保存dat文件 5.optparse #处理命令行参数 6.configpa ...
- HTTP服务介绍
摘自 https://mp.weixin.qq.com/s?__biz=MzI4NDM5NzE4Ng==&mid=2247484093&idx=1&sn=3d87e9772ff ...
- Day37 多进程
什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实体: ...
- python 爬爬爬 基本函数~
https://docs.python.org/2/howto/urllib2.html#data http://zhuoqiang.me/python-urllib2-usage.html #cod ...