第05天-文件操作相关函数
01.stat函数(重点)
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int stat(const char* path,struct stat *buf)
int lstat(const char *pathname,struct stat *buf)
功能:
获取文件状态信息
stat和lstat的区别:
当文件是一个符号链接时,lstat返回的是该符号链接本身的信息;
而stat返回的是该链接指定的文件的信息
参数:
path:文件名
buf:保存文件信息的结构体
返回值
成功:0;
失败:-1;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XdrXSGRQ-1656152623410)(D:\图片\image-20220625073954397.png)]
获取文件属性信息
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int main()
{
int ret=-1;
struct stat s;
ret=stat("txt",&s);
if(ret==-1)
{
perror("stat");
return 1;
}
printf("st_dev=%lu\n",s.st_dev);
printf("st_ino=%ld\n",s.st_ino);
return 0;
}
st_mode获取文件类型与权限的第一个版本
man 2 stat手册
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RwSiqGLs-1656152623411)(D:\图片\image-20220625083229032.png)]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
int show_file_type(struct stat *s)
{
switch(s->st_mode&S_IFMT){
case S_IFBLK:printf("block device块设备\n");break;
case S_IFCHR:printf("character device字符设备\n");break;
case S_IFDIR:printf("directory目录\n");break;
case S_IFIFO:printf("IS A FIFO管道\n");break;
case S_IFLNK:printf("IS A symLINK符号链接\n");break;
case S_IFREG:printf("IS A regular file普通文件\n");break;
case S_IFSOCK:printf("IS A SOCKET\n");break;
default :printf("unknown\n");break;
}
return 0;
}
int main(int argc,char *argv[])
{
struct stat s;
if(argc!=2)
{
printf("usage:./a.out filename");
return 1;
}
//获取文件信息
int ret=-1;
ret=stat(argv[1],&s);
if(ret==-1)
{
perror("stat");
return 1;
}
//显示文件类型
show_file_type(&s);
return 0;
}
第二个版本
int show_file_type_v2(struct stat *s)
{
if(S_ISREG(s->st_mode))
{
printf("is a regular file\n");
return 0;
}
if(S_ISLNK(s->st_mode))
{
printf("is a symblic link\n");
return 0;
}
if(S_ISBLK(s->st_mode))
{
printf("is a block device\n");
return 0;
}
if(S_ISDIR(s->st_mode))
printf("is a directory\n");
return 0;
}
if(S_ISCHR(s->st_mode))
{
printf("is a character devicce\n");
return 0;
}
if(S_ISFIFO(s->st_mode))
{
printf("is a FIFO\n");
return 0;
}
if(S_ISSOCK(s->st_mode))
{
printf("is a socket套接字\n");
return 0;
}
return 0;
}
文件权限
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o7mGUKDC-1656152623413)(D:\图片\image-20220625093211566.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WFyEeQgZ-1656152623414)(D:\图片\image-20220625093507666.png)]
access函数
chmod函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9hSaHIpB-1656152623415)(D:\图片\image-20220625094310470.png)]
chown函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vlX51xUl-1656152623416)(D:\图片\image-20220625094400639.png)]
truncate函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gDR1wt97-1656152623416)(D:\图片\image-20220625094429486.png)]
link函数
symlink函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kIaLHiRa-1656152623417)(D:\图片\image-20220625094523851.png)]
readlink函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eO9nPkGk-1656152623418)(D:\图片\image-20220625094601834.png)]
unlink函数
rename函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h9rrKCYp-1656152623418)(D:\图片\image-20220625094710678.png)]
02.文件描述符复制
dup函数
#include<unistd.h>
int dup(int oldfd);
功能:
通过oldfd复制出一个新的文件描述符,新的文件描述符是调用进程文件描述符表中最小可用的文件描述符,最终oldfd和新的文件描述符都指向同一个文件
参数:
oldfd:需要复制的文件描述符 oldfd
返回值:
成功:新文件描述符
失败:-1;
书上解释:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6sBzldeH-1656152623419)(D:\图片\image-20220625100258289.png)]
共用一个文件偏移量
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
int main()
{
//打开文件
int fd=-1;
fd=open("txt",O_RDWR|O_CREAT,0644);
if(fd==-1)
{
perror("open");
return 0;
}
//操作
int newfd=dup(fd);
if(newfd==-1)
{
perror("dup");
return 0;
}
write(fd,"asdfsd",6);
write(newfd,"12456",5);
//关闭文件
close(fd);
close(newfd);
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hO368tFx-1656152623419)(D:\图片\image-20220625101913209.png)]
dup2函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n1TLaZdi-1656152623420)(D:\图片\image-20220625102931102.png)]
dup2就可以直接完成以上操作
书上解释:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8k1y0cAo-1656152623421)(D:\图片\image-20220625103108205.png)]
fcnlt函数
复制文件描述符
#include<unistd.h>
#include<fcntl.h>
int fcntl(int fd,int cmd ,.../* arg*/);
功能:改变已打开的文件性质,fcntl针对描述符提供控制。
参数:
fd:操作的文件描述符
cmd:操作方式
arg:针对cmd的值,fcntl能够接受第三个参数int arg。
返回值:
成功:返回某个其他值
失败:-1
有五种函数功能:
1)复制一个现有的描述符(cmd=F_DUPFD)
2)获得/设置文件描述符标志(cmd=F_GETFD)
3)获得/设置文件状态标志(cmd=F_GETFL或F_SETFL)
4)获得/设置异步I/O所有权(cmd=F_GETOWN或FSETOWN)
5)获得/设置记录锁(cmd=F_GETLK,F_SETLK或F_SETLKW)
书上解释:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zw8RZxFM-1656152623422)(D:\图片\image-20220625110957038.png)]
getcwd函数
#include<unistd.h>
char* getcwd(char *buf,size_t size)
功能:获取当前进程目录
参数:
buf:缓存区,存储当前的工作目录
size:缓冲区大小
返回值:
成功:buf中保存当前进程工作目录为位置
失败:NULL
chdir函数
#include<unistd.h>
int chdir(const char*path)
功能: 修改当前进程
参数:
path:切换的路径
返回值:
成功:0
失败:-1
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int ret=-1;
char buf[128];
//获取当前路径
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
//修改路径
ret=chdir("/home/rui");
if(-1==ret)
{
perror("chdir");
return 1;
}
//获得路径
memset(buf,0,128);
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
return 0;
}
opendir函数
closedir函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r5UWEZ6i-1656152623422)(D:\图片\image-20220625175011901.png)]
readdir函数
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uwuddhUr-1656152623422)(D:\图片\image-20220625181026538.png)]
}
//获得路径
memset(buf,0,128);
if(NULL==getcwd(buf,128))
{
perror("getcwd");
return 1;
}
printf("buf:%s",buf);
return 0;
}
#### opendir函数
#### closedir函数
[外链图片转存中...(img-r5UWEZ6i-1656152623422)]
#### readdir函数
[外链图片转存中...(img-uwuddhUr-1656152623422)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FaCCjihx-1656152623423)(D:\图片\image-20220625181919233.png)]