一、Linux的IO函数 2
1.stat, lstat函数
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
作用:获取文件相关信息
kekecoder@kekecoder-virtual-machine:~/Linux/lesson12$ stat a.txt
文件:a.txt
大小:11 块:8 IO 块:4096 普通文件
设备:801h/2049d Inode:403715 硬链接:1
权限:(0664/-rw-rw-r–) Uid:( 1000/kekecoder) Gid:( 1000/kekecoder)
最近访问:2021-08-24 09:13:18.281722828 +0800
最近更改:2021-08-24 09:13:31.729457211 +0800
最近改动:2021-08-24 09:13:31.729457211 +0800
创建时间:-
参数:-pathmame:文件路径
-statbuf:结构体变量,传出参数,用于保存获取的文件信息
返回值:成功返回0,失败返回-1,设置error
结构体用来保存获取到的文件信息,封装到结构体变量
struct stat {
dev_t st_dev; // ID of device containing file
ino_t st_ino; //Inode number节点
mode_t st_mode; // File type and mode 存储文件类型
nlink_t st_nlink; // Number of hard links 硬链接数
uid_t st_uid; // User ID of owner
gid_t st_gid; // Group ID of owner 组ID
dev_t st_rdev; // Device ID (if special file)
off_t st_size; // Total size, in bytes
blksize_t st_blksize; // Block size for filesystem I/O
blkcnt_t st_blocks; // Number of 512B blocks allocated
}
int lstat(const char *pathname, struct stat *statbuf);
作用:获取软连接指向文件的信息
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdio.h>
int main()
{
struct stat statbuf;
int ret=stat("a.txt",&statbuf);
if(ret== -1)
{
perror("stat");
return -1;
}
printf("size: %ld\n", statbuf.st_size);
return 0;
}
2、模拟实现 ls-I 命令
//模拟实现ls -l 命令,写一个app程序,实现ls-l命令
//-rw-rw-r-- 1 kekecoder kekecoder 11 8月 24 09:13 a.txt
//size: 11
//-rw-rw-r-- 1 kekecoder kekecoder 11 Tue Aug 24 09:13:31 2021 a.txt
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<stdio.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<string.h>
int main(int argc, char *argv[])
{ //判断输入的参数是否正确
if(argc<2)
{
printf("%s filename\n",argv[0]);
return -1;
}
//通过stat 函数获取用户传入的文件的信息
struct stat st;
int ret=stat(argv[1],&st); //把信息保存到结构体当中
if(ret== -1)
{
perror("stat");
return -1;
}
//获取文件类型和文件权限
char perms [11]={0}; //用来保存文件类型和文件权限的字符串
switch (st.st_mode & S_IFMT)
{
case S_IFLNK:
perms[0]='l';
break;
case S_IFDIR:
perms[0]='d';
break;
case S_IFREG: //普通文件
perms[0]='-';
break;
case S_IFBLK: //块设备
perms[0]='b';
break;
case S_IFCHR: //字符设备
perms[0]='c';
break;
case S_IFSOCK: //套接字
perms[0]='s';
break;
case S_IFIFO: //管道
perms[0]='p';
break;
default:
perms[0]='?';
break;
}
//判断文件的访问权限
perms[1]=(st.st_mode & S_IRUSR) ? 'r' : '-';
perms[2]=(st.st_mode & S_IWUSR) ? 'w' : '-';
perms[3]=(st.st_mode & S_IXUSR) ? 'x' : '-';
//文件所在组权限
perms[4]=(st.st_mode & S_IRGRP) ? 'r' : '-';
perms[5]=(st.st_mode & S_IWGRP) ? 'w' : '-';
perms[6]=(st.st_mode & S_IXGRP) ? 'x' : '-';
//其他
perms[7]=(st.st_mode & S_IROTH) ? 'r' : '-';
perms[8]=(st.st_mode & S_IWOTH) ? 'w' : '-';
perms[9]=(st.st_mode & S_IXOTH) ? 'x' : '-';
//硬连接数
int linkNum=st.st_nlink;
//文件所在者
char * fileUser=getpwuid(st.st_uid)->pw_name;
//文件所在组
char * fileGrp =getgrgid(st.st_gid)->gr_name;
//文件大小
long int fileSize=st.st_size;
printf("size: %ld\n", st.st_size);
//获取修改的时间
char * time=ctime(&st.st_mtime);
char mtime[512]={0};
strncpy(mtime,time,strlen(time) -1);
//输出
char buf[1024];
sprintf(buf,"%s %d %s %s %ld %s %s",perms,linkNum,fileUser,fileGrp,fileSize,mtime,argv[1]);
printf("%s\n",buf);
return 0;
}
3、文件属性操作函数
access函数
#include <sys/stat.h>
#include <unistd.h>
int access(const char *pathname, int mode);
作用:判断某个文件是否有某个权限,或者文件是否存在
参数:
- pathname:判断的文件路径
- mode:
R_OK:是否有读权限
W_OK:判断文件是否有写权限
X_OK:判断文件是否有执行权限
F_OK:判断文件是否存在
返回值:成功返回0,失败-1;
/*
#include <sys/stat.h>
#include <unistd.h>
int access(const char *pathname, int mode);
作用:判断某个文件是否有某个权限,或者文件是否存在
参数:
- pathname:判断的文件路径
- mode:
R_OK:是否有读权限
W_OK:判断文件是否有写权限
X_OK:判断文件是否有执行权限
F_OK:判断文件是否存在
返回值:成功返回0,失败-1;
*/
#include <sys/stat.h>
#include<stdio.h>
int main()
{
int ret=access("a.txt");
if(ret == -1)
{
perror("access");
}
printf("文件存在!!!\n");
return 0;
}
chmod函数
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
作用:修改文件权限
参数:
-pathname:需要修改的文件路径
-mode:需要修改的权限值,八进制数
/*
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
作用:修改文件权限
参数:
-pathname:需要修改的文件路径
-mode:需要修改的权限值,八进制数
*/
#include <sys/stat.h>
#include <stdio.h>
int main()
{
int ret=chmod("a.txt",0775);
if(ret== -1)
{
perror("chmod");
return -1;
}
return 0;
}
truncate函数
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
作用:修改文件权限
参数:
-pathname:需要修改的文件路径
-mode:需要修改的权限值,八进制数
/*
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
作用:修改文件权限
参数:
-pathname:需要修改的文件路径
-mode:需要修改的权限值,八进制数
*/
*/
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
int main()
{
int ret=truncate("b.txt",20);
if(ret == -1)
{
perror("truncate");
return -1;
}
return 0;
}