7. Standard Document Library

1 Query file attributes

<sys/types.h>

<sys/stat.h>

int stat(const char *filename ,struct stat *buf) Get file attributes

int fstat(int fd,struct stat *buf) The function is the same as stat, but the input parameter is the file descriptor

int Istat(const char *filename,struct stat *buf) The function is the same as stat, but the connection file is processed tenderly

Query file properties

struct stat

dev_t st_dev //device ID

mode_t st_mode //File type and permissions (-rwx------ displayed by ls)

nlink_t st_nlink //Number of file links

uid_t st_uid //File owner ID

gid_t st_gid //The group ID to which the file belongs

off_t st_size //file size

time_t st_atime //Recent access time (access)

time_t st_mtime //Last modification time (modify)

time_t st_ctime //The last change time of the file status (change)

Query file properties

file type

S_IFREG normal file -

S_IFDIR directory file d

S_IFCHR character file c

S_IFBLK block file b

S_IFIFO pipe file p

S_IFLNK symbolic link l

S_IFSOCK socket file s

 

S_IFMT mask

Judgment method: if (st_mode & S_IFMT)

 

Query file properties

Macro judgment, return 0 or 1

S_ISREG(st_mode) /* Determine whether it is a normal file - */

S_ISDIR(st_mode) /* Determine whether it is a directory file d */

S_ISCHR(st_mode) /* Determine whether it is a character file c */

S_ISBLK(st_mode) /* Determine whether it is a block file b */

S_ISFIFO(st_mode) /* Determine whether it is a pipe file p */

S_ISLNK(st_mode) /* Determine whether it is a symbolic link l */

S_ISSOCK(st_mode) /*Determine whether the socket file is a bit*/

Determine file type:

 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 }

2 Query file access attributes

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325125644&siteId=291194637