赞
踩
最近在做cmu15-445的实验时,在源文件中第一次遇见了这个结构体,这里简要的记载一下其用法。
struct stat这个结构体是用来描述一个系统文件系统中的文件属性的结构.
- struct stat {
-
- mode_t st_mode; //文件对应的模式,文件,目录等
- ino_t st_ino; //inode节点号
- dev_t st_dev; //设备号码
- dev_t st_rdev; //特殊设备号码
- nlink_t st_nlink; //文件的连接数
- uid_t st_uid; //文件所有者
- gid_t st_gid; //文件所有者对应的组
- off_t st_size; //普通文件,对应的文件字节数
- time_t st_atime; //文件最后被访问的时间
- time_t st_mtime; //文件内容最后被修改的时间
- time_t st_ctime; //文件状态改变时间
-
- };
- // 文件类型位字段的S_IFMT 0170000 位掩码
- S_IFSOCK 0140000 套接字
-
- S_IFLNK 0120000 符号链接
-
- S_IFREG 0100000 常规文件
-
- S_IFBLK 0060000 块设备
-
- S_IFDIR 0040000 目录
-
- S_IFCHR 0020000 字符设备
-
- S_IFIFO 0010000 先进先出
-
- S_ISUID 0004000 用户id位
-
- S_ISGID 0002000 集组id位
-
- S_ISVTX 0001000 粘性位
-
- // 文件所有者权限掩码 S_IRWXU 00700
- S_IRUSR 00400 文件所有者具可读取权限
-
- S_IWUSR 00200 文件所有者具可写入权限
-
- S_IXUSR 00100 文件所有者具可执行权限
-
- S_IRWXG 00070 用户组的遮罩值(即所有权限值)
-
- S_IRGRP 00040 用户组具可读取权限
-
- S_IWGRP 00020 用户组具可写入权限
-
- S_IXGRP 00010 用户组具可执行权限
-
- S_IRWXO 00007 其他用户的遮罩值(即所有权限值)
-
- S_IROTH 00004 其他用户具可读取权限
-
- S_IWOTH 00002 其他用户具可写入权限
-
- S_IXOTH 00001 其他用户具可执行权限
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
#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:路径名的部分组件不是目录
- auto DiskManager::GetFileSize(const std::string &file_name) -> int {
- struct stat stat_buf;
- int rc = stat(file_name.c_str(), &stat_buf);
- return rc == 0 ? static_cast<int>(stat_buf.st_size) : -1;
- }
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- //int stat(const char *path, struct stat *buf);
-
- int main()
- {
-
- char *path = "test.c";
- struct stat sb;
-
- if (stat(path, &sb) == -1)
- {
- perror("stat");
- exit(EXIT_SUCCESS);
- }
-
- printf("文件类型: ");
-
- switch (sb.st_mode & S_IFMT) //st_mode进行”&”操作,从而就可以得到某些特定的信息。
- {
- case S_IFBLK: printf("块设备驱动\n");
- break;
- case S_IFCHR: printf("字符设备\n");
- break;
- case S_IFDIR: printf("目录\n");
- break;
- case S_IFIFO: printf("先入先出/管道\n");
- break;
- case S_IFLNK: printf("创建符号链接\n");
- break;
- case S_IFREG: printf("普通文件\n");
- break;
- case S_IFSOCK: printf("套接字\n");
- break;
- default: printf("未知数?\n");
- break;
- }
-
- printf("索引节点号: %ld\n", (long) sb.st_ino);
-
- printf("Mode: %lo (octal)\n",(unsigned long) sb.st_mode);
-
- printf("链接数: %ld\n", (long) sb.st_nlink);
- printf("所有权: UID=%ld GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);
-
- printf("首选I/O块大小: %ld bytes\n",(long) sb.st_blksize);
- printf("文件大小: %lld bytes\n",(long long) sb.st_size);
- printf("块分配: %lld\n",(long long) sb.st_blocks);
-
- printf("最后状态更改: %s", ctime(&sb.st_ctime));
- printf("最后的文件访问: %s", ctime(&sb.st_atime));
- printf("最后的文件修改: %s", ctime(&sb.st_mtime));
-
- exit(EXIT_SUCCESS);
-
- }
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
运行结果:
Linux中用st_mode判断文件类型 - 简书 (jianshu.com)
linux C/C++ struct stat示例_c++ struct stat-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。