【C/C++文件处理系列】struct stat 结构体定义

获取文件状态的函数 stat fstat lstat 都与struct stat 有关。函数原型如下,都定义在   sys/stat.h 中,原型如下

  int stat(const char *path, struct stat *buf);
  int fstat(int fd, struct stat *buf);
  int lstat(const char *path, struct stat *buf);
函数实现稍后整理。

###

struct stat的定义部分找了很久,最终在看 linux 系统命令  stat的手册中,找到了

SEE ALSO
       stat(2)

       The  full documentation for stat is maintained as a Texinfo manual.  

 man  2 stat ,得到 struct stat 的详细说明,介绍了 stat()  lstat()及 fstat() 的基本功能及struct stat的定义。

These  functions  return information about a file.  No permissions are required on the file itself, but — in
       the case of stat() and lstat() — execute (search) permission is required on all of the directories  in  path
       that lead to the file.

       stat() stats the file pointed to by path and fills in buf.

       lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not
       the file that it refers to.

       fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.

       All of these system calls return a stat structure, which contains the following fields:

           struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               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; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

           
st_dev: 描述文件归属的设备
st_rdev:描述文件inode 所代表的设备
st_size:文件字节数,如果是符号链接,则表示路径大小。
st_blocks :分配给该文件的块数,即512字节单位。不考虑空洞情况
st_blksize :文件系统的首先块大小。
st_atime:上次访问时间,访问文件的操作会改变该值 
st_mtime:上次修改时间, 
st_ctime:上次状态改变的时间,对文件的读写将改变该值。
###

linux 系统中 stat 命令 与ls 的默认执行如下

$
$stat stat.h
  File: `stat.h'
  Size: 16815     	Blocks: 40         IO Block: 4096   regular file
Device: 801h/2049d	Inode: 845263      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-01-28 18:09:35.000000000 +0800
Modify: 2015-01-28 18:09:35.000000000 +0800
Change: 2015-03-14 04:18:17.000000000 +0800
$
$ls -l stat.h 
-rw-r--r-- 1 root root 16815 Jan 28  2015 stat.h
$

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值