server-api-stat

原型

用于获取文件的状态。

1
2
3
// path:要获取的文件的文件路径
// buf:文件状态存储指针
int stat(const char* path, struct stat* buf);

相关结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct stat
{
dev_t st_dev; // 此文件所处设备的设备ID号
ino_t st_ino; // inode数值
mode_t st_mode; // 保护设置,定义在sys/stat.h
nlink_t st_nlink; // 硬链接数
uid_t st_uid; // 文件所有者的id
gid_t st_gid; // 文件所有者的组id
dev_t st_rdev; // 设备id
off_t st_size; // 以字节计算的大小
blksize_t st_blksize; // 文件系统的块大小
blkcnt_t st_blocks; // 占用的块的数量
time_t st_atime; // 最后一次使用过时间
time_t st_mtime; // 最后一次修改的时间
time_t st_ctime; // 最后一次状态变动时间
}

使用参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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

int main()
{
struct stat st;

if(-1 == stat("test.txt", &st)){
// 文件状态获取失败
printf("获取文件状态失败\n");
return -1;
}

printf("包含此文件的设备ID:%d\n", st.st_dev);
printf("此文件的节点:%d\n", st.st_ino);
printf("次文件的保护模式:%d\n", st.st_mode);

return 0;
}