zoukankan      html  css  js  c++  java
  • Linux 获取文件属性

    使用stat/lstat获取文件属性

    头文件:#include <sys/types.h>

        #include <sys/stat.h>

    int stat(const char  *path,struct  stat *struct_stat);
    int lstat(const char *path,struct stat *struct_stat);

    stat与lstat的区别:这两个方法区别在于stat没有处理字符链接(软链接)的能力,如果一个文件是符号链接,stat会直接返回它所
    指向的文件的属性;而lstat返回的就是这个符号链接的内容。

    两个函数的第一个参数都是文件的路径,第二个参数是struct stat的指针。返回值为0,表示成功执行。

    error:
    执行失败是,error被自动设置为下面的值:

    EBADF:   文件描述词无效
    EFAULT:   地址空间不可访问
    ELOOP:   遍历路径时遇到太多的符号连接
    ENAMETOOLONG:文件路径名太长
    ENOENT:  路径名的部分组件不存在,或路径名是空字串
    ENOMEM:  内存不足
    ENOTDIR:  路径名的部分组件不是目录

    当然除了以上方法,也可以通过文件描述符来获取文件的属性;

    int fstat(int fdp, struct stat *struct_stat); //通过文件描述符获取文件对应的属性。fdp为文件描述符

    struct stat:

    一个关于文件属性重要的结构体

    struct stat {
    mode_t st_mode; //文件对应的模式,文件,目录等
    ino_t st_ino; //inode节点号
    dev_t st_dev; //设备号码
    dev_t st_rdev; //特殊设备号码
    nlink_t st_nlink; //文件的硬链接数连接数
    uid_t st_uid; //文件所有者
    gid_t st_gid; //文件所有者对应的组
    off_t st_size; //普通文件,对应的文件字节数
    time_t st_atime; //文件最后被访问的时间
    time_t st_mtime; //文件内容最后被修改的时间
    time_t st_ctime; //文件属性inode改变时间
    blksize_t st_blksize; //文件内容对应的块大小
    blkcnt_t st_blocks; //文件内容对应的块数量
    };

    stat结构体中的 st_mode 则定义了下列数种情况:

    S_IFMT 0170000    文件类型的位遮罩
    S_IFLNK 0120000   符号连接
    S_IFREG 0100000   一般文件
    S_IFBLK 0060000   区块装置
    S_IFDIR 0040000   目录
    S_IFCHR 0020000    字符装置
    S_IFIFO 0010000    先进先出
    S_ISUID 04000   文件的(set user‐id on execution)位
    S_ISGID 02000   文件的(set group‐id on execution)位
    S_ISVTX 01000   文件的sticky位
    S_IRUSR 00400   文件所有者具可读取权限
    S_IWUSR 00200   文件所有者具可写入权限
    S_IXUSR 00100   文件所有者具可执行权限
    S_IRGRP 00040   用户组具可读取权限
    S_IWGRP 00020   用户组具可写入权限
    S_IXGRP 00010   用户组具可执行权限
    S_IROTH 00004   其他用户具可读取权限
    S_IWOTH 00002   其他用户具可写入权限
    S_IXOTH 00001   其他用户具可执行权限

    上述的文件类型在POSIX中定义了检查这些类型的宏定义
    例如:
    S_ISLNK (st_mode) 判断是否为符号连接
    S_ISREG (st_mode) 是否为一般文件
    S_ISDIR (st_mode) 是否为目录
    S_ISCHR (st_mode) 是否为字符设备文件
    S_ISSOCK (st_mode) 是否为socket

    文件属性获取

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int main(int argc,char **argv)
    {
        struct stat statbuff;
        if(argc<2)
        {
            printf("usage: %s [filename]",argv[0]);
            return -1;
        }
        lstat(argv[1],&statbuff);
        if(S_ISLNK(statbuff.st_mode))
            printf("%s is a link file
    ",argv[1]);
        /*
        * //也可以这么写
        if(S_IFLNK == statbuff.st_mode & S_IFMT)
            printf("%s is link file
    ",argv[1]);
        */
        if(S_ISREG(statbuff.st_mode))
            printf("%s is regular file
    ",argv[1]);
        if(S_ISDIR(statbuff.st_mode))
            printf("%s is directory file
    ",argv[1]);
        return 0;
    }
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    

    int main(int argc, char** argv) {   struct stat buf;   if(argc < 2)   {   printf("Usage: stat [filename] ");   return1;   }
      if(stat(argv[
    1], &buf) == ‐1)   {     perror("stat");     return1;   }
      printf(
    "access time %s ", ctime(&buf.st_atime));   printf("modify time %s ", ctime(&buf.st_mtime));   printf("change inode time %s ", ctime(&buf.st_ctime));   return 0; }
     
    char *ctime(const time_t *timep); //将时间转换成字符串
     
  • 相关阅读:
    SDWebImage 原理及使用问题
    iOS沙盒目录
    java基础知识积累总结
    切片原型[start:stop:step]
    select嵌套问题
    Numpy:字符串转为数值型、日期型;日期型转为数值型
    数据标准化处理(简单整理)
    Numpy:自定义复合数据类型
    Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性
    Numpy:使用索引、切片提取一维数组、多维数组内数据
  • 原文地址:https://www.cnblogs.com/wangkeqin/p/9255876.html
Copyright © 2011-2022 走看看