linux环境中如何在程序中获取文件属性
可以调用stat,fstat,lstat函数
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
/*执行成功都返回0,当有错误发生时则返回-1,错误代码存到errno中*/
对于符号链接文件,lstat返回的是符号链接文件本身的状态信息,而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;