当前位置:   article > 正文

struct stat结构体_struct stat 结构体

struct stat 结构体

简介:

最近在做cmu15-445的实验时,在源文件中第一次遇见了这个结构体,这里简要的记载一下其用法。

功能:

struct stat这个结构体是用来描述一个系统文件系统中的文件属性的结构.

结构内容:

  1. struct stat {
  2. mode_t st_mode; //文件对应的模式,文件,目录等
  3. ino_t st_ino; //inode节点号
  4. dev_t st_dev; //设备号码
  5. dev_t st_rdev; //特殊设备号码
  6. nlink_t st_nlink; //文件的连接数
  7. uid_t st_uid; //文件所有者
  8. gid_t st_gid; //文件所有者对应的组
  9. off_t st_size; //普通文件,对应的文件字节数
  10. time_t st_atime; //文件最后被访问的时间
  11. time_t st_mtime; //文件内容最后被修改的时间
  12. time_t st_ctime; //文件状态改变时间
  13. };
  1. // 文件类型位字段的S_IFMT 0170000 位掩码
  2. S_IFSOCK 0140000 套接字
  3. S_IFLNK 0120000 符号链接
  4. S_IFREG 0100000 常规文件
  5. S_IFBLK 0060000 块设备
  6. S_IFDIR 0040000 目录
  7. S_IFCHR 0020000 字符设备
  8. S_IFIFO 0010000 先进先出
  9. S_ISUID  0004000  用户id位
  10. S_ISGID 0002000 集组id位
  11. S_ISVTX 0001000 粘性位
  12. // 文件所有者权限掩码 S_IRWXU 00700
  13. S_IRUSR    00400    文件所有者具可读取权限
  14. S_IWUSR    00200    文件所有者具可写入权限
  15. S_IXUSR    00100    文件所有者具可执行权限
  16. S_IRWXG    00070    用户组的遮罩值(即所有权限值)
  17. S_IRGRP    00040    用户组具可读取权限
  18. S_IWGRP    00020    用户组具可写入权限
  19. S_IXGRP    00010    用户组具可执行权限
  20. S_IRWXO    00007    其他用户的遮罩值(即所有权限值)
  21. S_IROTH    00004    其他用户具可读取权限
  22. S_IWOTH    00002    其他用户具可写入权限
  23. S_IXOTH    00001    其他用户具可执行权限

具体使用

依赖的头文件

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

使用的函数

int stat(const char *path, struct stat *buf);

第一个参数是文件的路径,第二个参数是struct stat的指针。

  • 执行成功时,返回值为0
  • 执行失败时,error被自动设置为下面的值:

    • EBADF: 文件描述词无效
    • EFAULT: 地址空间不可访问
    • ELOOP: 遍历路径时遇到太多的符号连接
    • ENAMETOOLONG:文件路径名太长
    • ENOENT:路径名的部分组件不存在,或路径名是空字串
    • ENOMEM:内存不足
    • ENOTDIR:路径名的部分组件不是目录
  1. auto DiskManager::GetFileSize(const std::string &file_name) -> int {
  2. struct stat stat_buf;
  3. int rc = stat(file_name.c_str(), &stat_buf);
  4. return rc == 0 ? static_cast<int>(stat_buf.st_size) : -1;
  5. }

实例

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. //int stat(const char *path, struct stat *buf);
  8. int main()
  9. {
  10. char *path = "test.c";
  11. struct stat sb;
  12. if (stat(path, &sb) == -1)
  13. {
  14. perror("stat");
  15. exit(EXIT_SUCCESS);
  16. }
  17. printf("文件类型: ");
  18. switch (sb.st_mode & S_IFMT) //st_mode进行”&”操作,从而就可以得到某些特定的信息。
  19. {
  20. case S_IFBLK: printf("块设备驱动\n");
  21. break;
  22. case S_IFCHR: printf("字符设备\n");
  23. break;
  24. case S_IFDIR: printf("目录\n");
  25. break;
  26. case S_IFIFO: printf("先入先出/管道\n");
  27. break;
  28. case S_IFLNK: printf("创建符号链接\n");
  29. break;
  30. case S_IFREG: printf("普通文件\n");
  31. break;
  32. case S_IFSOCK: printf("套接字\n");
  33. break;
  34. default: printf("未知数?\n");
  35. break;
  36. }
  37. printf("索引节点号: %ld\n", (long) sb.st_ino);
  38. printf("Mode: %lo (octal)\n",(unsigned long) sb.st_mode);
  39. printf("链接数: %ld\n", (long) sb.st_nlink);
  40. printf("所有权: UID=%ld GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);
  41. printf("首选I/O块大小: %ld bytes\n",(long) sb.st_blksize);
  42. printf("文件大小: %lld bytes\n",(long long) sb.st_size);
  43. printf("块分配: %lld\n",(long long) sb.st_blocks);
  44. printf("最后状态更改: %s", ctime(&sb.st_ctime));
  45. printf("最后的文件访问: %s", ctime(&sb.st_atime));
  46. printf("最后的文件修改: %s", ctime(&sb.st_mtime));
  47. exit(EXIT_SUCCESS);
  48. }

运行结果:

参考链接:

Linux中用st_mode判断文件类型 - 简书 (jianshu.com)

linux C/C++ struct stat示例_c++ struct stat-CSDN博客

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

闽ICP备14008679号