玩转Linux 001
struct stat
是一个在 C 语言中用于表示文件或文件系统信息的结构体。它通常用于与系统调用和库函数一起使用,以获取有关文件的各种属性。
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
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; /* Block size for filesystem I/O */
blkcnt_t st_blocks; /* Number of 512B blocks allocated */
};
struct stat {
dev_t st_dev; // 文件的设备编号
ino_t st_ino; // 文件的 inode 编号
mode_t st_mode; // 文件的类型和权限
nlink_t st_nlink; // 连接到文件的硬链接数
uid_t st_uid; // 文件所有者的用户 ID
gid_t st_gid; // 文件所有者的组 ID
off_t st_size; // 文件大小(以字节为单位)
time_t st_atime; // 文件的最后访问时间
time_t st_mtime; // 文件的最后修改时间
time_t st_ctime; // 文件的状态最后更改时间
blksize_t st_blksize; // 文件系统块的最佳大小
blkcnt_t st_blocks; // 文件占用的块数
};
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{
struct stat buf;
stat("1.txt",&buf);
printf("文件的大小 为:%ld\n",buf.st_size);
return 0;
}
运行截图
在这个示例中,stat
函数用于获取指定文件的信息,并将结果存储在 struct stat
类型的结构体 buf
中。然后,我们打印了文件的大小。