Linux文件和目录操作函数


2021年5月22日,袁老、吴老走了,愿他们在下一个轮回里平安幸福。
一路走好!!!国士无双,英雄千古!!!

1. 文件操作

1.1 stat/lstat函数

1.1.1 描述

  • stat:获取文件属性(从inode上获得)
  • 函数原型:int stat(const char *pathname,struct stat *buf);struct stat
  • 能够穿透(跟踪)符号链接
  • 返回值
    • 成功:0
    • 失败:-1
struct stat {
   
    dev_t         st_dev;       //文件的设备编号
    ino_t         st_ino;       //节点
    mode_t        st_mode;      //重点:文件的类型和存取的权限
    nlink_t       st_nlink;     //连到该文件的硬连接数目,刚建立的文件值为1
    uid_t         st_uid;       //重点:用户ID
    gid_t         st_gid;       //重点:组ID
    dev_t         st_rdev;      //(设备类型)若此文件为设备文件,则为其设备编号
    off_t         st_size;      //重点:文件字节数(文件大小)
    blksize_t     st_blksize;   //块大小(文件系统的I/O 缓冲区大小)
    blkcnt_t      st_blocks;    //块数
    time_t        st_atime;     //最后一次访问时间
    time_t        st_mtime;     //重点:最后一次修改时间
    time_t        st_ctime;     //最后一次改变时间(指属性)
};

1.1.2 获取文件大小

在这里插入图片描述

s_mode:

  • 该变量占 2byte 共 16位

  • 掩码的使用:st_mode & 掩码

  • 其他人权限(0-2 bit)

    • S_IROTH 00004 读权限
    • S_IWOTH 00002 写权限
    • S_IXOTH 00001 执行权限
    • S_IRWXO 00007 掩码,过滤st_mode中除其他人权限以外的信息
  • 所属组权限(3-5 bit)

    • S_IRGRP 00040 读权限
    • S_IWGRP 00020 写权限
    • S_IXGRP 00010 执行权限
    • S_IRWXG 00070 掩码,过滤st_mode中除所属组权限以外的信息
  • 文件所有者权限(6-8 bit)

    • S_IRUSR 00400 读权限
    • S_IWUSR 00200 写权限
    • S_IXUSR 00100 执行权限
    • S_IRWXU 00700 掩码,过滤st_mode中除文件所有者权限以外的信息
  • 特殊权限位(9-11 bit)

    • S_ISUID 0004000 设置用户ID
    • S_ISGID 0002000 设置组ID
    • S_ISVTX 0001000 黏住位
  • 文件类型(12-15 bit)

    • S_IFSOCK 0140000 套接字
    • S_IFLNK 0120000 符号链接(软链接)
    • S_IFREG 0100000 普通文件
    • S_IFBLK 0060000 块设备
    • S_IFDIR 0040000 目录
    • S_IFCHR 0020000 字符设备
    • S_IFIFO 0010000 管道
    • S_IFMT 0170000 掩码,过滤st_mode中除文件类型以外的信息
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

int main(){
   
	struct stat st;
	int ret = stat("test.txt",&st);
	if(ret == -1){
   
		perror("stat error");
		exit(1);
	}
	cout << "filesize is " << st.st_size << endl;
	return 0;
}

1.1.3 判断文件类型

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

using namespace std;

int main(){
   
	struct stat st;
	int ret = stat("test.txt",&st);
	if(ret == -1){
   
		perror("stat error");
		exit(1);
	}
	//输出文件大小
	cout << "filesize is " << st.st_size << endl;
	//判断文件类型
	long int filetype = (st.st_mode & S_IFMT);
	if(filetype == S_IFREG){
   
		cout << "普通文件" << endl;
	}

	//文件所有者操作权限
	if(st.st_mode & S_IRUSR){
   
		cout << "  r  " << endl;
	}
	if(st.st_mode & S_IWUSR){
   
		cout << "  w  " << endl;
	}
	if(st.st_mode & S_IXUSR){
   
		cout << "  x  " << endl;
	}

	return 0;
}

1.1.4 lstat函数

  • 不穿透(跟踪)符号链接
  • 与stat区别
    • 在读取软链接文件时,lstat读取的是链接文件本身的属性
    • stat读取的是链接文件指向的文件的属性
      请添加图片描述

1.2 access函数

  • 测试当前用户指定文件是否具有某种权限
    • 当前用户:使用哪个用户调用这个函数,这个用户就是当前用户。
  • 函数原型int access(const char *pathname, int mode);
    • 参数:
      • pathname:文件名
      • mode(权限类别、4种)
        • R_OK 读
        • W_OK 写
        • X_OK 执行
        • F_OK 文件是否存在
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值