#includepathname : 用于指定一个需要查看属性的文件路径。 buf : struct stat 类型指针,用于指向一个 struct stat 结构体变量。调用 stat 函数的时候需要传入一个 struct stat 变量的指针,获取到的文件属性信息就记录在 struct stat 结构体中 。 返回值: 成功返回 0 ;失败返回 -1 ,并设置 error 。#include #include int stat(const char *pathname, struct stat *buf);
示例代码:
获取文件的 inode 节点编号以及文件大小,并将它们打印出来。
#include#include #include #include #include int main(void) { struct stat file_stat; int ret; ret = stat("./test_file", &file_stat); if (-1 == ret) { perror("stat error"); exit(-1); } printf("file size: %ld bytesn" "inode number: %ldn", file_stat.st_size, file_stat.st_ino); exit(0); }
测试验证:
从图中可以得知,此文件的大小为 4060 个字节,inode 编号为 656929
接下来编译测试程序,并运行



