stat函数

stat函数

表头文件:    #include <sys/stat.h> 
函数定义:    int stat(const char *file_name, struct stat *buf); 
函数说明:    通过文件名filename获取文件信息,并保存在buf所指的结构体stat中 
返回值:     执行成功则返回0,失败返回-1,错误代码存于errno(需要include <errno.h>)

errno错误代码: 
    ENOENT         参数file_name指定的文件不存在 
    ENOTDIR        路径中的目录存在但却非真正的目录 
    ELOOP          欲打开的文件有过多符号连接问题,上限为16符号连接 
    EFAULT         参数buf为无效指针,指向无法存在的内存空间 
    EACCESS        存取文件时被拒绝 
    ENOMEM         核心内存不足 
    ENAMETOOLONG   参数file_name的路径名称太长

stat结构体定义:

struct stat { 
    dev_t         st_dev;       //文件的设备编号 
    ino_t         st_ino;         //节点 
    mode_t        st_mode;   //文件的类型和存取的权限 
    nlink_t       st_nlink;      //连到该文件的硬连接数目,刚建立的文件值为1 
    uid_t         st_uid;        //用户ID 
    gid_t         st_gid;        //组ID 
    dev_t         st_rdev;     //(设备类型)若此文件为设备文件,则为其设备编号 
    off_t         st_size;       //文件字节数(文件大小) 
    unsigned long st_blksize;   //块大小(文件系统的I/O 缓冲区大小) 
    unsigned long st_blocks;   //块数 
    time_t        st_atime;      //最后一次访问时间 
    time_t        st_mtime;     //最后一次修改时间 
    time_t        st_ctime;     //最后一次改变时间(指属性) 

};

#include  <sys/stat.h>
#include <iostream>
#include <errno.h>
using namespace std;

int main()
{

struct stat buf;
int res=stat("himat.cpp",&buf);

if(res!=0)
{

std::cout<<"open file is fail"<<std::endl;
//cout<<res<<endl;
}
else
{
 cout<<buf.st_dev<<endl;
cout<<buf.st_size<<endl;
cout<<buf.st_uid<<endl;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值