当前位置:   article > 正文

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

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的手册中,找到了

  1. SEE ALSO
  2. stat(2)
  3. The full documentation for stat is maintained as a Texinfo manual.

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

  1. These functions return information about a file. No permissions are required on the file itself, but — in
  2. the case of stat() and lstat() — execute (search) permission is required on all of the directories in path
  3. that lead to the file.
  4. stat() stats the file pointed to by path and fills in buf.
  5. lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not
  6. the file that it refers to.
  7. fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.
  8. All of these system calls return a stat structure, which contains the following fields:
  9. struct stat {
  10. dev_t st_dev; /* ID of device containing file */
  11. ino_t st_ino; /* inode number */
  12. mode_t st_mode; /* protection */
  13. nlink_t st_nlink; /* number of hard links */
  14. uid_t st_uid; /* user ID of owner */
  15. gid_t st_gid; /* group ID of owner */
  16. dev_t st_rdev; /* device ID (if special file) */
  17. off_t st_size; /* total size, in bytes */
  18. blksize_t st_blksize; /* blocksize for file system I/O */
  19. blkcnt_t st_blocks; /* number of 512B blocks allocated */
  20. time_t st_atime; /* time of last access */
  21. time_t st_mtime; /* time of last modification */
  22. time_t st_ctime; /* time of last status change */
  23. };

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

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

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

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/523209
推荐阅读
相关标签
  

闽ICP备14008679号