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

相关文章:

  • 2021-06-02
  • 2021-10-08
  • 2021-11-26
  • 2022-03-05
  • 2022-12-23
  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-27
  • 2022-12-23
  • 2021-10-28
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
相关资源
相似解决方案