Linux获取文件信息函数stat

1. stat函数

可获取文件的信息,包括文件类型、文件权限、文件大小等,如下:


函数原型:

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

int stat(const char* pathname, struct stat* buf);
int lstat(const char* pathname, struct stat* buf);
/*
功能:
    获取文件pathname的信息,放到stat结构体中。
    stat和lstat的区别:
        文件是一个符合链接时,lstat返回符号链接本身的信息;而stat返回该链接指向文件的信息。
参数:
    pathname:文件路径
    buf:保存文件信息的结构体
返回值:
    成功:0
    失败:-1
*/

stat简单使用示例:

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

int test() {
    int ret = -1;
    struct stat s;

    // 获取指定文件信息
    ret = stat("txt", &s);
    if (-1 == ret) {
        perror("stat");
        return 1;
    }

    // 文件属性信息
    printf("st_dev: %lu\n", s.st_dev); // %x:16进制,加#就有0x前缀
    printf("st_info: %ld\n", s.st_ino); // 
    printf("st_nlink: %lu\n", s.st_nlink);
    printf("st_uid: %d\n", s.st_uid);
    printf("st_gid: %d\n", s.st_gid);
    printf("st_size: %ld\n", s.st_size);
}

运行结果:


stat获取文件类型:

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

// 显示文件类型 (方式一)
int show_file_type01(struct stat* s) {
    switch (s->st_mode & __S_IFMT) {  // 固定格式
    case __S_IFREG:
        printf("该文件为普通文件.\n");
        break;
    case __S_IFDIR:
        printf("该文件为目录.\n");
        break;
    case __S_IFBLK:
        printf("该文件为块设备.\n");
        break;
    case __S_IFCHR:
        printf("该文件为字符设备.\n");
        break;
    case __S_IFSOCK:
        printf("该文件为套接字.\n");
        break;
    case __S_IFIFO:
        printf("该文件为管道.\n");
        break;
    case __S_IFLNK:
        printf("该文件为符号链接.\n");
        break;
    default:
        printf("未知文件类型.\n");
        break;
    }
    return 0;
}

// 显示文件类型 (方式二)
int show_file_type02(struct stat* s) {
    if (S_ISREG(s->st_mode)) {
        printf("该文件为普通文件.\n");
    } else if (S_ISDIR(s->st_mode)) {
        printf("该文件为目录.\n");
    } else if (S_ISBLK(s->st_mode)) {
        printf("该文件为块设备.\n");
    } else if (S_ISCHR(s->st_mode)) {
        printf("该文件为字符设备.\n");
    } else if (S_ISSOCK(s->st_mode)) {
        printf("该文件为套接字.\n");
    } else if (S_ISFIFO(s->st_mode)) {
        printf("该文件为管道.\n");
    } else if (S_ISLNK(s->st_mode)) {
        printf("该文件为符号链接.\n");
    } else {
        printf("未知文件类型.\n");
    }
    return 0;
}

int test01(int argc, const char* argv[]) {
    int ret = -1;
    struct stat* s = NULL;
    s = (struct stat*)malloc(sizeof(struct stat));  // 注意需要给其分配内存
    if(s==NULL) {
        perror("malloc");
        return 1;
    }

    if (2 != argc) { // 若命令格式不是 ./a.out filename
        printf("usage: ./a.out filename\n");
        return 1;
    }

    // 获取文件信息
    ret = stat(argv[1], s);  // argv[0]为 "./a.out"
    if (-1 == ret) {
        perror("stat");
        return 1;
    }

    show_file_type01(s);
    show_file_type02(s);
    free(s);  // 释放内存
    return 0;
}

int main(int argc, const char* argv[]) {
    test01(argc, argv);
    return 0;
}

运行结果:

st_mode文件权限:


stat获取文件权限:

// 显示文件权限 rwx
int show_permission(struct stat* s) { // 固定格式
    /* 判断文件所属者权限 */
    if (s->st_mode & S_IRUSR) { //读权限
        printf("r");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IWUSR) { // 写权限
        printf("w");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IXUSR) { // 执行权限
        printf("x");
    } else {
        printf("-");
    }

    /* 判断文件所属组权限 */
    if (s->st_mode & S_IRGRP) { //读权限
        printf("r");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IWGRP) { // 写权限
        printf("w");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IXGRP) { // 执行权限
        printf("x");
    } else {
        printf("-");
    }

    /* 判断其他人的权限 */
    if (s->st_mode & S_IROTH) { //读权限
        printf("r");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IWOTH) { // 写权限
        printf("w");
    } else {
        printf("-");
    }

    if (s->st_mode & S_IXOTH) { // 执行权限
        printf("x");
    } else {
        printf("-");
    }
    printf("\n");
}

void test03(const char* argv[]) {
    int ret = -1;
    struct stat s;
    ret = stat(argv[1], &s);
    show_permission(&s);
}

int main(int argc, const char* argv[]) {
    if (2 != argc) {
        printf("usage: ./a.out filename");
    }
    test03(argv);
    return 0;
}

运行结果: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morgan歪比巴卜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值