前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stat函数的使用说明[通俗易懂]

stat函数的使用说明[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-24 13:42:57
8370
发布2022-09-24 13:42:57
举报

大家好,又见面了,我是你们的朋友全栈君。

1:stat函数

取得指定文件的文件属性,文件属性存储在结构体stat里

代码语言:javascript
复制
#include <sys/stat.h>
int stat(const char *pathname, struct stat *statbuf);

2:结构体stat

代码语言:javascript
复制
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 */
               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 */

               /* Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */

           #define st_atime st_atim.tv_sec /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };

st_dev:设备ID,不太常用

st_ino:【inode】,【inode】是啥?不知道就看上面关于【inode】的解释

st_mode:文件的类型和权限,共16位,如下图。

0-11位控制文件的权限

12-15位控制文件的类型

0-2比特位:其他用户权限

3-5比特位:组用户权限

6-8比特位:本用户权限

9-11比特位:特殊权限

12-15比特位:文件类型(因为文件类型只有7中,所以用12-14位就够

文件类型的宏如下(下面的数字是8进制):

S_IFSOCK 0140000 socket S_IFLNK 0120000 symbolic link(软连接) S_IFREG 0100000 regular file(普通文件) S_IFBLK 0060000 block device(块设备文件) S_IFDIR 0040000 directory(目录) S_IFCHR 0020000 character device(字符设备文件) S_IFIFO 0010000 FIFO(管道) st_nlink:硬连接计数

st_uid:这个文件所属用户的ID

st_gid:这个文件所属用户的组ID

st_rdev:特殊设备的ID,不太常用

st_size:文件的大小

st_blksize:不明是干啥的

st_blocks:不明是干啥的

struct timespec st_atim:最后访问的时间

struct timespec st_mtim:最后修改的时间

struct timespec st_ctim:最后状态改变的时间

3:示例

代码语言:javascript
复制
/************************************************************************* > File Name: stat.c > Author: kayshi > Mail: kayshi2019@qq.com > Created Time: Sat 17 Oct 2020 04:13:17 PM CST ************************************************************************/

#include <stdio.h>
#include <sys/stat.h>

#define FILE_N "/home/kayshi/code/Test/test.txt"

void main(void)
{ 
   
    struct stat file_stat;

    stat(FILE_N, &file_stat);
    printf("%ld", file_stat.st_size);
}

获得文件text.txt的大小

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171796.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1:stat函数
  • 2:结构体stat
  • 3:示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档