1 查询文件属性
<sys/types.h>
<sys/stat.h>
int stat(const char *filename ,struct stat *buf) 获取文件属性
int fstat(int fd,struct stat *buf) 功能同stat,但是入参是文件描述符
int Istat(const char *filename,struct stat *buf) 功能同stat,但是嫩处理连接文件
查询文件属性
struct stat
dev_t st_dev //设备ID
mode_t st_mode //文件类型与权限(ls显示的 –rwx------)
nlink_t st_nlink //文件链接数
uid_t st_uid //文件所有人 ID
gid_t st_gid //文件所属组 ID
off_t st_size //文件大小
time_t st_atime //最近访问时间(access)
time_t st_mtime //最近修改时间(modify)
time_t st_ctime //文件状态最近改变时间(change)
查询文件属性
文件类型
S_IFREG 普通文件 -
S_IFDIR 目录文件 d
S_IFCHR 字符文件 c
S_IFBLK 块文件 b
S_IFIFO 管道文件 p
S_IFLNK 符号链接 l
S_IFSOCK 套接字文件 s
S_IFMT 掩码
判断方法: if (st_mode & S_IFMT)
查询文件属性
宏判断, 返回 0或者1
S_ISREG(st_mode) /* 判断是否为普通文件 - */
S_ISDIR(st_mode) /* 判断是否为目录文件 d */
S_ISCHR(st_mode) /* 判断是否为字符文件 c */
S_ISBLK(st_mode) /* 判断是否为块文件 b */
S_ISFIFO(st_mode) /* 判断是否为管道文件 p */
S_ISLNK(st_mode) /* 判断是否为符号链接 l */
S_ISSOCK(st_mode) /*判断是否位套接字文件*/
判断文件类型:
1 #include <stdio.h> 2 #include <sys/stat.h> 3 4 int main(int argc,char **argv) 5 { 6 if(argc == 1) 7 { 8 return 0; 9 } 10 11 struct stat stInfo; 12 13 int ret = stat(argv[1],&stInfo); 14 15 if(ret!=0) 16 { 17 perror("Fail to stat"); 18 return ret; 19 } 20 21 switch(stInfo.st_mode & S_IFMT) 22 { 23 case S_IFREG: 24 printf("Regulat file\n"); 25 break; 26 case S_IFDIR: 27 printf("Directory file\n"); 28 break; 29 case S_IFCHR: 30 printf("Charactor file\n"); 31 break; 32 case S_IFBLK: 33 printf("BLock file\n"); 34 break; 35 case S_IFIFO: 36 printf("FIFO file\n"); 37 break; 38 case S_IFLNK: 39 printf("link file\n"); 40 break; 41 case S_IFSOCK: 42 printf("Sock file\n"); 43 break; 44 default: 45 printf("Unknow file\n"); 46 break; 47 } 48 49 return 0; 50 }