Linux下文件属性的获取

1. 数据结构和系统调用


在Linux下进行C/C++编程,主要通过以下三个系统调用来获取文件(普通文件,目录,管道,socket,字符,块等)属性。

头文件“#include <sys/stat.h>”

(1) //通过文件名称获取文件属性
int stat(const char *restrict pathname, struct stat *restrict buf); 

(2) //通过文件描述符获取文件属性
int fstat(int filedes, struct stat *buf); 

(3) //通过符号文件名称获取文件属性
int lstat(const char *restrict pathname, struct stat *restrict buf);

返回值(三个函数一样)
成功:0
失败:-1

三个系统调用的区别:
1. fstat接受的第一个参数是“文件描述符”,stat和lstat是“文件全路径”,文件描述符需要用调用open之后才能得到,文件全路经直接写即可;

2. lstat获取的是该符号链接本身的信息;而stat获取的是该链接指向的文件的信息;

这三个系统调用都依赖一个重要的结构体struct stat

  1. struct stat {  
  2.         mode_t     st_mode;       //文件对应的模式,文件,目录等  
  3.         ino_t      st_ino;        //inode节点号  
  4.         dev_t      st_dev;        //设备号码  
  5.         dev_t      st_rdev;       //特殊设备号码  
  6.         nlink_t    st_nlink;      //文件的连接数  
  7.         uid_t      st_uid;        //文件所有者  
  8.         gid_t      st_gid;        //文件所有者对应的组  
  9.         off_t      st_size;       //普通文件,对应的文件字节数  
  10.         time_t     st_atime;      //文件最后被访问的时间  
  11.         time_t     st_mtime;      //文件内容最后被修改的时间  
  12.         time_t     st_ctime;      //文件状态改变时间  
  13.         blksize_t st_blksize;     //文件内容对应的块大小  
  14.         blkcnt_t   st_blocks;     //伟建内容对应的块数量  
  15.       };  

2. 示例程序

(1) 先在测试目录下建立一个test.txt文件,然后"ln -s test.txt link"建立一个符号文件指向test.txt;


(2) 写测试代码

  1. #include <sys/stat.h>  
  2. #include <fcntl.h>  
  3. #include "stdio.h"  
  4. #include "time.h"  
  5. #include "unistd.h"  
  6.   
  7. void report(struct stat *ptr)  
  8. {  
  9.     if(!ptr)  
  10.     {  
  11.         return;  
  12.     }  
  13.           
  14.     printf("The device no is: %d\n", ptr->st_dev);  
  15.     printf("The file's node number is: %d\n", ptr->st_ino);  
  16.     printf("The file's access mode is: %d\n", ptr->st_mode);  
  17.     printf("The file's hard link number is: %d\n", ptr->st_nlink);  
  18.     printf("The file's user id is: %d\n", ptr->st_uid);  
  19.     printf("The file's group id is: %d\n", ptr->st_gid);  
  20.     printf("The file's size is: %d\n", ptr->st_size);  
  21.     printf("The block size is: %d\n", ptr->st_blksize);  
  22.     printf("The number of allocated blocks is: %d\n", ptr->st_blocks);  
  23.   
  24.     struct tm* pAccesstime=localtime(&(ptr->st_atime));  
  25.     struct tm* pModifytime=localtime(&(ptr->st_mtime));  
  26.     struct tm* pChangetime=localtime(&(ptr->st_ctime));  
  27.     char aBuffer[64] = {0};  
  28.     char mBuffer[64] = {0};  
  29.     char cBuffer[64] = {0};  
  30.     strftime(aBuffer, 64, "The last access time is: %Y-%m-%d %H:%M:%S \n", pAccesstime);  
  31.     strftime(mBuffer, 64, "The last modify time is: %Y-%m-%d %H:%M:%S \n", pModifytime);  
  32.     strftime(cBuffer, 64, "The last change time is: %Y-%m-%d %H:%M:%S \n", pChangetime);  
  33.   
  34.     printf(aBuffer);  
  35.     printf(mBuffer);  
  36.     printf(cBuffer);  
  37. }  
  38.   
  39. void Test(const char* pFileName)  
  40. {  
  41.     if(!pFileName)  
  42.     {  
  43.         printf("error: file pointer is null in Test function\n");  
  44.         return;  
  45.     }  
  46.     printf("File %s is be in testing ...\n", pFileName);  
  47.   
  48.     struct stat st;  
  49.     int nRev_st = stat(pFileName, &st);  
  50.     if(nRev_st < 0)  
  51.     {  
  52.         printf("File %s is not existed \n", pFileName);  
  53.     }  
  54.     else  
  55.     {  
  56.         printf("------get file %s info by stat \n", pFileName);  
  57.         report(&st);  
  58.     }  
  59.   
  60.     struct stat ls;  
  61.     int nRev_ls = lstat(pFileName, &ls);  
  62.     if(nRev_ls < 0)  
  63.     {  
  64.         printf("File %s is not existed \n", pFileName);  
  65.     }  
  66.     else  
  67.     {  
  68.         printf("------get file %s info by lstat \n", pFileName);  
  69.         report(&ls);  
  70.     }  
  71.   
  72.     struct stat fs;  
  73.     int fd = open(pFileName, O_RDONLY);  
  74.     int nRev_fs = fstat(fd, &fs);  
  75.     close(fd);  
  76.     if(nRev_fs < 0)  
  77.     {  
  78.         printf("File %s is not existed \n", pFileName);  
  79.     }  
  80.     else  
  81.     {  
  82.         printf("------get file %s info by fstat \n", pFileName);  
  83.         report(&fs);  
  84.     }  
  85. }  
  86.   
  87. int main()  
  88. {  
  89.     const char* pFileName_Real = "test.txt";  
  90.     const char* pFileName_Link = "link";  
  91.     Test(pFileName_Real);  
  92.     printf("\n\n");  
  93.     Test(pFileName_Link);  
  94.     return 0;  
  95. }  


(3) 运行结果






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值