Fork me on GitHub

S_ISREG、S_ISDIR等系列宏的功能

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<unistd.h>
 4 #include<string.h>
 5 #include<errno.h>
 6 #include<sys/types.h>
 7 #include<sys/stat.h>
 8 #include<fcntl.h>
 9 
10 int main(int arg, char *args[])
11 {
12 
13 //    char s[]="abc.txt";
14     int fd = open(args[1],O_RDONLY);//用读写追加的方式打开文件
15     if(fd==-1)
16         printf("err id %s\n",strerror(errno));
17     else
18     {
19         printf("success fd =%d\n",fd);
20 
21         struct stat buf;
22         fstat(fd,&buf);
23         if(S_ISREG(buf.st_mode))//判断文件是否为标准文件
24         {
25             printf("%s is charfile\n",args[1]);
26         }
27         if(S_ISDIR(buf.st_mode))//判断文件是否为目录
28         {
29             printf("%s is dir\n",args[1]);
30         }
31 
32         printf("%s size = %d\n",args[1],buf.st_size);//判断文件的大小
33         close(fd);
34 
35     }
36     return 0;
37 }

 fstat和stat的功能基本一致,只是函数中的参数不同

posted @ 2016-07-20 15:54  千秋此意  阅读(3139)  评论(0编辑  收藏  举报