学习stat,lstat,fstat1

来源:互联网 发布:中商情报网数据库 编辑:程序博客网 时间:2024/05/23 16:56

学习stat,lstat,fstat1 函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。函数原型#include <sys/stat.h>

int stat(const char *restrict pathname, struct stat *restrict buf);提供文件名字,获取文件对应属性。int fstat(int filedes, struct stat *buf);通过文件描述符获取文件对应的属性。int lstat(const char *restrict pathname, struct stat *restrict buf);连接文件描述命,获取文件属性。2 文件对应的属性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;     //伟建内容对应的块数量      };可以通过上面提供的函数,返回一个结构体,保存着文件的信息

函数名: lstat 功  能: 获取一些文件相关的信息 用  法: int lstat(const char *path, struct stat *buf);        参数: path:文件路径名。filedes:文件描述词。buf:是以下结构体的指针struct stat {      dev_t     st_dev;     /* 文件所在设备的标识 */       ino_t     st_ino;     /* 文件结点号 */     mode_t    st_mode;    /* 文件保护模式 */     nlink_t   st_nlink;   /* 硬连接数 */     uid_t     st_uid;     /* 文件用户标识 */     gid_t     st_gid;     /* 文件用户组标识 */     dev_t     st_rdev;    /* 文件所表示的特殊设备文件的设备标识 */     off_t     st_size;    /* 总大小,字节为单位 */     blksize_t st_blksize; /* 文件系统的块大小 */     blkcnt_t st_blocks;   /* 分配给文件的块的数量,512字节为单元 */     time_t    st_atime;   /* 最后访问时间 */     time_t    st_mtime;   /* 最后修改时间 */     time_t    st_ctime;   /* 最后状态改变时间 */};返回说明: 成功执行时,返回0。失败返回-1,errno被设为以下的某个值 EBADF: 文件描述词无效EFAULT: 地址空间不可访问ELOOP: 遍历路径时遇到太多的符号连接ENAMETOOLONG:文件路径名太长ENOENT:路径名的部分组件不存在,或路径名是空字串ENOMEM:内存不足ENOTDIR:路径名的部分组件不是目录 程序例: lstat( szFilePath, &buf) < 0#include <sys/stat.h>fstat/stat/lstat系统调用int stat(const char *path, struct stat *buf);int fstat(int filedes, struct stat *buf);int lstat(const char *path, struct stat *buf);int stat(const char *restrict pathname,struct stat *restrict buf);int fstat(int fields,struct stat *buf);int lstat(const char *restrict pathname,struct stat *restrict buf);一旦给出pathname:stat函数就返回与此命名文件有关的信息结构,fstat函数获取已在描述符fields上打开文件的有关信息。lstat函数类似于stat.但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用文件的信息。第二个参数buf是指针,它指向一个我们必须提供的结构,这些函数填写由buf指向的结构。struct stat{    mode_t st_mode;     //文件类型和权限信息    ino_t   st_ino;      //i结点标识    dev_t   st_dev;      //device number (file system)    dev_t   st_rdev;     //device number for special files    nlink_t st_nlink;    //符号链接数    uid_t   st_uid;      //用户ID    gid_t   st_gid;      //组ID    off_t   st_size;     //size in bytes,for regular files    time_t st_st_atime; //最后一次访问的时间    time_t st_mtime;    //文件内容最后一次被更改的时间    time_t st_ctime;    //文件结构最后一次被更改的时间    blksize_t st_blksize; //best I/O block size    blkcnt_t st_blocks; //number of disk blocks allocated   };文件类型:普通文件,目录文件,块特殊文件,字符特殊文件,套接字,FIFO,符号链接.文件类型信息包含在stat结构的st_mode成员中,可以用如下的宏确定文件类型,这些宏是stat结构中的st_mode成员.S_ISREG();S_ISDIR();S_ISCHR();S_ISBLK();S_ISFIFO();S_ISLNK();S_ISSOCK()

struct stat buf;        char * ptr;  if(lstat(argv,&buf)<0)  if (S_ISREG(buf.st_mode))                ptr="普通文件";        if (S_ISDIR(buf.st_mode))                ptr="目录";

示例:     #i nclude<iostream>     int main(int argc,char* argv[])     {          int i;        struct stat buf;        char * ptr;                for(i=1;i<argc;i++)         {            if(lstat(argv,&buf)<0)               {                 perror("错误原因是:");                 continue;               }            if (S_ISREG(buf.st_mode))                ptr="普通文件";            if (S_ISDIR(buf.st_mode))                ptr="目录";                        //......and so on...                       cout<<"参数为:"<<argv<<"的标识是一个"<         }        exit(0);     }

 

原创粉丝点击