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的功能基本一致,只是函数中的参数不同

转载于:https://www.cnblogs.com/leejxyz/p/5688648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值