Linux 函数--fstat/stat/lstat系统调用

本文介绍了如何使用fstat、stat及lstat系统调用来获取文件的相关信息,包括文件类型、权限、大小、创建时间等,并提供了示例代码。此外,还详细解释了如何通过st_mode成员判断文件的具体类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【fstat/stat/lstat系统调用】  
   
功能描述:  
获取一些文件相关的信息。
 
用法:  
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
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:路径名的部分组件不是目录  

 

 

 文件和目录
                       stat,fstat和lstat函数
 
 #i nclude<sys/stat.h>
 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);
 返回值:若成功则返回0,失败则返回-1
 
 一旦给出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()
 
  示例:
     #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[i],&buf)<0)
               {
                 perror("错误原因是:");
                 continue;
               }

            if (S_ISREG(buf.st_mode))
                ptr="普通文件";
            if (S_ISDIR(buf.st_mode))
                ptr="目录";
            
            //......and so on...
            
           cout<<"参数为:"<<argv[i]<<"的标识是一个"<<ptr<<endl;
         }
        exit(0);
     }
[gitlab-runner@c ~]$ npm install gitbook-cli -g npm WARN deprecated q@1.5.0: You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other. npm WARN deprecated npm WARN deprecated (For a CapTP with native promises, see @endo/eventual-send and @endo/captp) /home/gitlab-runner/.npm-global/bin/gitbook -> /home/gitlab-runner/.npm-global/lib/node_modules/gitbook-cli/bin/gitbook.js + gitbook-cli@2.3.2 added 578 packages from 672 contributors in 7.112s [gitlab-runner@c ~]$ gitbook fetch 3.2.3 Installing GitBook 3.2.3 (node:2048289) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Socket]. Use emitter.setMaxListeners() to increase limit (node:2048289) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Socket]. Use emitter.setMaxListeners() to increase limit /home/gitlab-runner/.npm-global/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287 if (cb) cb.apply(this, arguments) ^ TypeError: cb.apply is not a function at /home/gitlab-runner/.npm-global/lib/node_modules/gitbook-cli/node_modules/npm/node_modules/graceful-fs/polyfills.js:287:18 at FSReqCallback.oncomplete (fs.js:169:5) [gitlab-runner@c ~]$ gitbook install info: nothing to install! [gitlab-runner@c ~]$
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值