十三、linux编程中目录IO常用编程函数
概念:
索引节点,Inode是Index Node的缩写,存储于文件系统上的任何文件都可以用索引节点来表示,所以也可以说索引节点是整个linux文件系统的基础。操作系统在读取硬盘的时候不是一个块一个块的来读取信息,因为这样做的话效率太低,文件数据都储存在“块”中,那么很显然,我们还必须找到一个地方储存文件的元信息,比如文件的创建者、文件的创建日期、文件的大小等等。这种储存文件元信息的区域就叫做inode,中文译名为“索引节点”。
在Linux系统中,文件系统主要分为两部分,一部分为元数据(metadata),另一部分为数据本身。元数据,换句话说,就是包含了数据有关信息的数据。索引节点就管理着文件系统中元数据的部分。
一、stat函数组(获取文件信息,具体看stat结构体 )
1、头文件、函数原型及相关结构体(可以通过man 2 stat 打开对应的stat帮助文档)
/*所需头文件*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*函数原型*/
int stat(const char *path, struct stat *buf);
– 参数*path:文件路径
– 参数*buf:文件信息
– 返回值:成功为0,否则为-1
int fstat(int fd, struct stat *buf);
– 参数fd:文件描述符
– 参数*buf:文件信息
– 返回值:成功为0,否则为-1
int lstat(const char *path, struct stat *buf);
– 参数*path:文件路径
– 参数*buf:返回文件的信息,针对符号链接,lstat 返回链接本身,而不是而非目标文件
– 返回值:成功为0,否则为-1stat结构体包含对象:struct stat {dev_t st_dev; /* ID of device containing file */ino_t st_ino; /* inode number */mode_t st_mode; /* protection */nlink_t st_nlink; /* number of hard links */uid_t st_uid; /* user ID of owner */gid_t st_gid; /* group ID of owner */dev_t st_rdev; /* device ID (if special file) */off_t st_size; /* total size, in bytes */blksize_t st_blksize; /* blocksize for file system I/O */blkcnt_t st_blocks; /* number of 512B blocks allocated */time_t st_atime; /* time of last access */time_t st_mtime; /* time of last modification */time_t st_ctime; /* time of last status change */};
2、具体使用方法及代码:
#include <stdio.h>
//通过man文档可以查看到stat函数组头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>//open函数的参数头文件
#include <fcntl.h>int main(int argc,char *argv[])
{struct stat groupstat;int fd,ret;if(argc <2){printf("\nPlease input file path\n");return 1;}//stat函数测试 ret = stat(argv[1],&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("stat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);//fstat函数测试fd = open(argv[1],O_RDWR|O_NOCTTY|O_NDELAY);if(fd<0){printf("Please make sure file path\n");return 1;}ret = fstat(fd,&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("fstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);//lstat函数测试 ret = lstat(argv[1],&groupstat);if(ret){printf("Please make sure file path\n");return 1;}printf("lstat function test , %s of st_ino inode is %ld\n",argv[1],groupstat.st_ino);return 0;
}
二、chmod函数组(设置文件权限 )
1、头文件、函数原型及相关结构体(可以通过man 2 chmod打开对应的chmod帮助文档)
/*头文件*/
#include <sys/stat.h>
/*函数原型*/
int chmod(const char *path, mode_t mode);// 修改指定路径的文件权限
– 参数*path:文件路径。
– 参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这个参数含义类似,也可以使用文档中的组合值。
– 返回值:成功返回0,错误返回-1。
int fchmod(int fd, mode_t mode);// 修改打开的文件权限
– 参数fd:文件描述符。
– 参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这个参数含义类似,也可以使用文档中的组合值。
– 返回值:成功返回0,错误返回-1。
2、具体使用方法及代码:
#include <stdio.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main(int argc,char *argv[])
{int fd,ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//chmod函数测试 ret = chmod(argv[1],0777);if(ret<0){printf("Please make sure file path\n");return 1;}printf("chmod %s 0777 is success!\n",argv[1]);//fchmod函数测试 fd = open(argv[2],O_RDWR|O_NOCTTY|O_NDELAY);if(fd<0){printf("Please make sure file path\n");return 1;}ret = fchmod(fd,0555);if(ret<0){printf("Please make sure file path\n");return 1;}printf("fchmod %s 0555 is success!\n",argv[1]);return 0;
}
三、获取当前目录
1、头文件、函数原型及相关结构体(可以通过man 3 getcwd打开对应的getcwd帮助文档)
/*头文件*/
#include <unistd.h>
/*函数原型*/
char *getcwd(char *buf, size_t size);
– 参数*buf:保存当前目录的缓冲区
– 参数size:在现代linux 中,buf 的长度至少可以为255 字节
– 返回值:成功返回指向当前目录的指针,和buf 的值一样,错误返回NULL
char *getwd(char *buf);
– 参数*buf:保存当前目录的缓冲区
– 返回值:成功返回指向当前目录的指针,和buf 的值一样,错误返回NULL
char *get_current_dir_name(void);
– 参数:无
– 返回值:成功返回指向当前目录的指针,错误返回NULL
2、具体使用方法及代码:
#include <stdio.h>
/*getcwd、getwd和get_current_dir_name
函数的头文件*/
#define __USE_GNU
#include <unistd.h>#define LENTH 255
int main()
{char pwd[LENTH];char *wd;
//getcwd函数测试 if(!getcwd(pwd,LENTH)){perror("getcwd");return 1;}printf("\ngetcwd pwd is %s\n",pwd);//getwd函数测试wd = getwd(pwd);if(!wd){perror("getcwd");return 1;}printf("\ngetwd pwd is %s\n",wd);//get_current_dir_name函数测试 wd = get_current_dir_name();if(!wd){perror("getcwd");return 1;}printf("\nget_current pwd is %s\n",wd);return 0;
}
四、创建目录
1、头文件、函数原型及相关结构体(可以通过man 2 mkdir打开对应的mkdir帮助文档)
/*头文件*/
#include <sys/stat.h>
#include <sys/types.h>
/*函数原型*/
int mkdir(const char *pathname, mode_t mode);
– 参数:文件路径
– 参数mode:直接使用数字即可。和前面命令中chmod 777 xxx 中的777 这个参数含义类似,也可以使用文档中的组合值。
– 返回值:成功返回0,错误返回-1。
2、具体使用方法及代码:
#include <stdio.h>
//mkdir函数头文件
#include <sys/stat.h>
#include <sys/types.h>int main(int argc,char *argv[])
{int ret;//检测参数 if(argc <2){printf("\nPlease input file path\n");return 1;}
//使用mkdir函数新建目录ret = mkdir(argv[1],0777);if(ret<0){printf("mkdir %s failed!\n",argv[1]);return 1;}printf("mkdir %s suceces!\n",argv[1]);return 0;
}
五、删除目录
1、头文件、函数原型及相关结构体(可以通过man 2 rmdir打开对应的rmdir帮助文档)
/*头文件*/
#include <unistd.h>
/*函数原型*/
int rmdir(const char *pathname);
– 参数*pathname:文件和目录的路径
– 返回值:成功返回0,错误返回-1
2、具体使用方法及代码:
#include <stdio.h>
//rmdir函数头文件
#include <unistd.h>int main(int argc,char *argv[])
{int ret;//检测参数 if(argc <2){printf("\nPlease input file path\n");return 1;}
//使用rmdir函数删除目录ret = rmdir(argv[1]);//目录要是绝对目录if(ret<0){printf("rmdir %s failed!\n",argv[1]);return 1;}printf("rmdir %s suceces!\n",argv[1]);return 0;
}
六、改变当前工作目录
1、头文件、函数原型及相关结构体(可以通过man 2 chdir打开对应的chdir帮助文档)
在实际应用中,代码可能需要从当前目录中进到其它目录,这个时候首先需要使用 getcwd函数获取当前目录,保存起来,然后使用 chdir 跳到其它目录,完成操作,然后再使用 chdir回到最初保存的目录。
#include <unistd.h>
/*函数原型*/
int chdir(const char *path);- 参数 *path:文件路径- 返回值:成功返回 0,错误返回-1。
int fchdir(int fd);- 参数 fd:open 函数返回的句柄,文件描述符。- 返回值:成功返回 0,错误返回-1。
2、具体使用方法及代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//chdir和fchdir函数头文件
#include <unistd.h>#define LENTH 255int main(int argc,char *argv[])
{int ret;char pwd[LENTH];//检测参数 if(argc <3){printf("\nPlease input file path\n");return 1;}//getcwd函数获取当前目录 if(!getcwd(pwd,LENTH)){perror("getcwd");return 1;}printf("\ngetcwd pwd is %s\n",pwd);//使用chdir函数转入其他目录ret = chdir(argv[1]);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",argv[1]);//转入其他目录,完成操作
//使用rmdir函数删除目录ret = rmdir(argv[2]);if(ret<0){printf("rmdir %s failed!\n",argv[2]);return 1;}printf("rmdir %s is success!\n",argv[2]);//再次使用chdir回到pwd保存的目录ret = chdir(pwd);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",pwd);return 0;
}
七、opendir和closedir目录
1、头文件、函数原型及相关结构体(可以通过man 3 opendir(closedir)打开对应的opendir(closedir)帮助文档)
前面介绍open和close函数用于打开关闭文件,这里介绍的opendir和closedir用于打开目录,相当于ls命令。
#include <sys/types.h>
#include <dirent.h>
/*函数原型*/
DIR *opendir(const char *name);
– 参数:目录的路径。
– 返回值:成功返回指向目录流的指针,错误返回NULL
int closedir(DIR *dirp);
– 参数:opendir 返回的dir 指针
– 返回值:成功返回0, 失败返回-1
2、具体使用方法及代码:
#include <stdio.h>
//opendir和closedir函数头文件
#include <dirent.h>
#include <sys/types.h>int main(int argc,char *argv[])
{int ret;DIR *dir;//检测参数 if(argc <2){printf("\nPlease input file path\n");return 1;}
//使用opendir函数打开目录dir = opendir(argv[1]);if(dir==NULL){printf("opendir %s failed!\n",argv[1]);return 1;}printf("opendir %s suceces!\n",argv[1]);
//使用closedir函数关闭目录closedir(dir);return 0;
}
八、readdir读取目录信息
1、头文件、函数原型及相关结构体(可以通过man 3 readdir打开对应的readdir帮助文档)
在前面使用opendir打开目录的基础上,可以使用readdir读取目录信息。
#include <dirent.h>
/*函数原型*/
struct dirent *readdir(DIR *dirp);
– 参数dirp:opendir 函数打开目录后返回的文件指针。
– 返回值:成功返回指向dirp 的指针dirent ,错误返回NULL。struct dirent {ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all file system types */char d_name[256]; /* filename */};
2、具体使用方法及代码:
#include <stdio.h>
//opendir,closedir,readdir函数头文件
#include <dirent.h>
#include <sys/types.h>int main(int argc,char *argv[])
{int ret;DIR *dir;struct dirent *catlog;
//检测参数 if(argc <2){printf("\nPlease input file path\n");return 1;}
//使用opendir函数打开目录dir = opendir(argv[1]);if(dir==NULL){printf("opendir %s failed!\n",argv[1]);return 1;}printf("opendir %s suceces!\n",argv[1]);
//使用readdir读取目录argv[1]catlog = readdir(dir);if(catlog == NULL){printf("readdir %s failed!\n",argv[1]);return 1;}printf("%s d_ino is %ld\n ",argv[1],catlog->d_ino);
//使用closedir函数关闭目录closedir(dir);return 0;
}
九、硬链接和软链接(符号链接)
1、硬链接
在Linux系统中,多个文件名指向同一索引节点(Inode)是正常且允许的。一般这种链接就称为硬链接。硬链接的作用之一是允许一个文件拥有多个有效路径名,这样用户就可以建立硬链接到重要的文件,以防止“误删”源数据。(换句话说就是我们前面讲得,文件的属性和数据是分开的,将两份不一样的文件属性同时指向同一份文件数据。那么这两份文件属性就是硬链接。)
硬链接函数
• int link(const char *oldpath, const char *newpath)
– 参数*oldpath:已有的文件路径。
– 参数*newpath:新建的硬链接文件路径。
– 返回值:成功返回0,错误返回-1。
#include <stdio.h>
//link函数头文件
#include <unistd.h>int main(int argc,char *argv[])
{int ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//测试link函数ret = link(argv[1],argv[2]);if(ret){printf("link failed");return 1;}printf("link %s to %s success!\n",argv[1],argv[2]);return 0;
}
2、软链接
软链接(也叫符号链接),类似于windows系统中的快捷方式,与硬链接不同,软链接就是一个普通文件,只是数据块内容有点特殊,文件用户数据块中存放的内容是另一文件的路径名的指向,通过这个方式可以快速定位到软连接所指向的源文件实体。软链接可对文件或目录创建。
软链接函数
• int symlink(const char *oldpath, const char *newpath)
– 参数*oldpath:已有的文件路径
– 参数*newpath:新建的符号链接文件路径
– 返回值:成功返回0,错误返回-1
#include <stdio.h>
//symlink函数头文件
#include <unistd.h>int main(int argc,char *argv[])
{int ret;if(argc <3){printf("\nPlease input file path\n");return 1;}//测试symlink函数ret = symlink(argv[1],argv[2]);if(ret){printf("symlink failed");return 1;}printf("symlink %s to %s success!\n",argv[1],argv[2]);return 0;
}
3、解除链接
解除链接函数
• int unlink(const char *pathname)
– 参数*pathname:链接文件的路径
– 返回值:成功返回0,错误返回-1
– unlink指向软链接,删除软链接;指向最后一个硬链接,相当于删除文件。因为硬链接的文件数据至少要有一个硬链接,如果最后一个都被删除,那么相当于文件数据也要删除了。
#include <stdio.h>
//unlink函数头文件
#include <unistd.h>int main(int argc,char *argv[])
{int ret;if(argc <2){printf("\nPlease input file path\n");return 1;}//测试unlink函数ret = unlink(argv[1]);if(ret){printf("unlink failed");return 1;}printf("unlink %s is success!\n",argv[1]);return 0;
}
十、文件拷贝
linux中,没有专用的文件拷贝函数,而是通过从旧文件读取(read函数),写入(write函数)新文件,来实现文件的拷贝。
#include <stdio.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>//argv[1] is oldpath ; argv[2] is newpath
#define LENTH 1024
int main(int argc,char *argv[])
{int fds,fdt;char buffer[LENTH];char *fileold,*filenew;fileold = argv[1];filenew = argv[2];if(argc <3){printf("\nPlease input file path\n");return 1;}//打开oldpath fds = open(fileold,O_RDWR);if(fds<0){printf("Please make sure file path\n");return 1;}//打开newpath,如果没有则创建目标文件fdt = open(filenew,O_WRONLY|O_CREAT);if(fdt<0){printf("Please make sure file path\n");return 1;}//读和写操作while(read(fds,buffer,LENTH)){write(fdt,buffer,strlen(buffer));}//关闭文件close(fds);close(fdt);printf("cp to finished!\n");printf("cp %s to %s success!\n",fileold,filenew);return 0;
}
十一、文件移动
移动文件命令为mv,函数为rename。
文件移动函数:
int rename(const char *oldpath, const char *newpath)
– 参数*oldpath:旧的文件路径
– 参数*newpath:新的文件路径
– 返回值:成功返回0,错误返回-1
#include <stdio.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>//argv[1] is oldpath ; argv[2] is newpath
int main(int argc,char *argv[])
{int ret;if(argc < 3){printf("\nPlease input file path\n");return 1;}if(ret = rename(argv[1],argv[2])){printf("\nerr\n");}printf("rename %s to %s success!\n",argv[1],argv[2]);return 0;
}
十三、linux编程中目录IO常用编程函数相关推荐
- 嵌入式学习之Linux系统编程---9 目录IO之readdir函数
1.readdir函数的函数原型 #include <dirent.h> struct dirent *readdir(DIR *dirp); 对于readdir函数来说,它只有目录流指针 ...
- linux 版本的scipy,Linux 系统中 SciPy (Python 3) 编程环境
Linux 系统中 SciPy (Python 3) 编程环境 SciPy (pronounced "Sigh Pie") is a Python-based ecosystem ...
- java当中有关循环的代码_有关Java循环的内容,编程中还是比较常用的,下面分享给大家几个循环的示例代码,练习一下。1、循环输出1到100之间所有能被3或能被4整除的数。pack...
有关Java循环的内容,编程中还是比较常用的,下面分享给大家几个循环的示例代码,练习一下. 1.循环输出1到100之间所有能被3或能被4整除的数. package com.hz.loop02; /** ...
- Linux 系统中 SciPy (Python 3) 编程环境
Linux 系统中 SciPy (Python 3) 编程环境 SciPy (pronounced "Sigh Pie") is a Python-based ecosystem ...
- Linux 之 Linux/Ubuntu 中开发操作中常用的命令整理
Linux 之 Linux/Ubuntu 中开发操作中常用的命令整理 目录 Linux 之 Linux/Ubuntu 中开发操作中常用的命令整理 一.简单介绍 二.常用命令 1. 打开终端 :Ctrl ...
- 原始套接字编程”中的Teardrop代码编程
原始套接字编程"中的Teardrop代码编程 (1)实验代码: #include <stdio.h> #include <stdlib.h> #include < ...
- 在Windows和Linux系统中,有以下常用的磁盘修复命令
在Windows和Linux系统中,有以下常用的磁盘修复命令: Windows系统: 1. CHKDSK命令:检查磁盘上的文件系统错误,并对损坏的扇区进行恢复.格式:CHKDSK [驱动器:] [参数 ...
- windows系统中创建线程常用哪几个函数,有什么区别?
windows系统中创建线程常用哪几个函数? 在windows系统中创建线程的函数有: CreadThread,AfxBeginThread,_beginthread,_beginthreadex. ...
- SQL Server 中截取字符串常用的函数
SQL Server 中截取字符串常用的函数:1.LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要截 ...
最新文章
- 我的日常工具——gdb篇
- android 之 Activity管理与Intent的六大属性
- spring spring c3p0 mysql配置_Spring通过c3p0配置bean连接数据库
- shell中的变量与eval(转)
- 怎么确定迭代器后面还有至少两个值_学会迭代和迭代器,让你的程序更省内存...
- react-native全局变量和静态变量使用
- Table边框使用总结
- 从入门到入土:基于Python爬取四川大学所有官方网站|狗头保命|
- ui自动化测试框架_自动化测试框架--Instrumentation
- L1-015. 跟奥巴马一起画方块-PAT团体程序设计天梯赛GPLT
- Facebook究竟是怎么抄袭Snapchat的?听听他们自己怎么说
- 服务器开机后显示f1 f2,电脑开机总是提示按f1 f2问题的解决办法
- Matlab 2016a 安装包及破解教程
- 数字图像处理MFC程序设计之图像的打开显示
- 刚安装3dsmax2020无法保存文件或注册机无法patch
- LumaQQ开发文档
- asio几种异步编程模型
- nali工具解析ip来源
- 漆学军:均线交叉,金叉做多,死叉做空的例子程序
- 文件流转换为base64码 和 base64码转换为文件流