zoukankan      html  css  js  c++  java
  • 关于文件结构体的使用

    使用这个结构体和方法时,需要引入:

    <sys/types.h>

    <sys/stat.h>

    struct stat这个结构体是用来描述一个linux系统文件系统中的文件属性的结构。

    可以有两种方法来获取一个文件的属性:

    1、通过路径:

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

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

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

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

    EBADF: 文件描述词无效

    EFAULT: 地址空间不可访问

    ELOOP: 遍历路径时遇到太多的符号连接

    ENAMETOOLONG:文件路径名太长

    ENOENT:路径名的部分组件不存在,或路径名是空字串

    ENOMEM:内存不足

    ENOTDIR:路径名的部分组件不是目录

    这两个方法区别在于stat没有处理字符链接(软链接)的能力,如果一个文件是符号链接,stat会直接返回它所指向的文件的属性;而lstat返
    回的就是这个符号链接的内容。这里需要说明一下的是软链接和硬链接的含义。我们知道目录在linux中也是一个文件,文件的内容就是这这个目录下面所有文
    件与inode的对应关系。那么所谓的硬链接就是在某一个目录下面将一个文件名与一个inode关联起来,其实就是添加一条记录!而软链接也叫符号链接更
    加简单了,这个文件的内容就是一个字符串,这个字符串就是它所链接的文件的绝对或者相对地址。

    2、通过文件描述符

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

    下面是这个结构的结构

    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; //文件状态改变时间

    blksize_t st_blksize; //文件内容对应的块大小

    blkcnt_t st_blocks; //伟建内容对应的块数量

    };
    stat结构体中的st_mode 则定义了下列数种情况:
    S_IFMT 0170000 文件类型的位遮罩
    S_IFSOCK 0140000 scoket
    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(S_IREAD) 00400 文件所有者具可读取权限
    S_IWUSR(S_IWRITE)00200 文件所有者具可写入权限
    S_IXUSR(S_IEXEC) 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_ISBLK (s3e) 是否为先进先出
    S_ISSOCK (st_mode) 是否为socket
    若一目录具有sticky位(S_ISVTX),则表示在此目录下的文件只能被该文件所有者、此目录所有者或root来删除或改名,在linux中,最典型的就是这个/tmp目录啦。

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1. //! 需要包含de头文件
    2.  
       
    3.  
      #include <sys/types.h>
    4.  
       
    5.  
      #include <sys/stat.h>
    6.  
       
    7.  
      int stat(const char *filename, struct stat *buf); //! prototype,原型
    8.  
       
    9.  
      struct stat
    10.  
      {
    11.  
       
    12.  
      dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
    13.  
       
    14.  
      ino_t st_ino; /* inode number -inode节点号*/
    15.  
       
    16.  
      mode_t st_mode; /* protection -保护模式?*/
    17.  
       
    18.  
      nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
    19.  
       
    20.  
      uid_t st_uid; /* user ID of owner -user id*/
    21.  
       
    22.  
      gid_t st_gid; /* group ID of owner - group id*/
    23.  
       
    24.  
      dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
    25.  
       
    26.  
      off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
    27.  
       
    28.  
      blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
    29.  
       
    30.  
      blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
    31.  
       
    32.  
      time_t st_atime; /* time of last access -最近存取时间*/
    33.  
       
    34.  
      time_t st_mtime; /* time of last modification -最近修改时间*/
    35.  
       
    36.  
      time_t st_ctime; /* time of last status change - */
    37.  
       
    38.  
      };
    1.  
      #include <iostream>
    2.  
       
    3.  
      #include <ctime>
    4.  
       
    5.  
      #include <sys/types.h>
    6.  
       
    7.  
      #include <sys/stat.h>
    8.  
       
    9.  
      using namespace std;
    10.  
       
    11.  
      int
    12.  
      main ()
    13.  
      {
    14.  
      struct stat buf;
    15.  
       
    16.  
      int result;
    17.  
       
    18.  
      result = stat ("./Makefile", &buf);
    19.  
       
    20.  
      if (result != 0)
    21.  
      {
    22.  
      perror ("Failed ^_^");
    23.  
      }
    24.  
      else
    25.  
      {
    26.  
       
    27.  
      //! 文件的大小,字节为单位
    28.  
       
    29.  
      cout << "size of the file in bytes: " << buf.st_size << endl;
    30.  
       
    31.  
      //! 文件创建的时间
    32.  
       
    33.  
      cout << "time of creation of the file: " << ctime (&buf.st_ctime) <<
    34.  
       
    35.  
      endl;
    36.  
       
    37.  
      //! 最近一次修改的时间
    38.  
       
    39.  
      cout << "time of last modification of the file: " <<
    40.  
       
    41.  
      ctime (&buf.st_mtime) << endl;
    42.  
       
    43.  
      //! 最近一次访问的时间
    44.  
       
    45.  
      cout << "time of last access of the file: " << ctime (&buf.st_atime)
    46.  
       
    47.  
      << endl;
    48.  
      }
    49.  
       
    50.  
      return 0;
    51.  
       
    52.  
      }



    $ ./test


    size of the file in bytes: 36


    time of creation of the file: Sun May 24 18:38:10 2009


    time of last modification of the file: Sun May 24 18:38:10 2009


    time of last access of the file: Sun May 24 18:38:13 2009

  • 相关阅读:
    网页 js 获取DPI pxTomm
    利用自定义属性实现js点击事件 委托
    鼠标移动div时禁止选中div中的文字的方法
    关于if简写语句优化的方法
    .clearfix::after(清除浮动)
    js 事件委托
    清空共享池
    oracle中scott用户权限不足
    安装完Oracle 12C数据库,scott账户过期,解锁方法
    Vulnhub靶场-Me Tomcat Host 学习笔记
  • 原文地址:https://www.cnblogs.com/WAsbry/p/12950672.html
Copyright © 2011-2022 走看看