文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>stat函数

stat函数

时间:2010-04-23  来源:sjtlqy

学习,stat,lstat,fstat 1 函数都是获取文件(普通文件,目录,管道,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;     //伟建内容对应的块数量
      };
可以通过上面提供的函数,返回一个结构体,保存着文件的信息。
下面是一个例子
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <time.h>
#include <unistd.h>
  
 #define TIME_STRING_BUF   58
char * timeString(time_t t,char *buf)
 {
 struct tm  *local;
local=localtime(&t) ;
strftime(buf,TIME_STRING_BUF,"%c",local);
return buf;
}


int statFile(const char * file)
{
struct  stat  statbuf;
char timeBuf[TIME_STRING_BUF];
if (lstat(file,&statbuf))
{
fprintf(stderr,"could not lstat %s:%s\n",file,strerror(errno));
return 1;
}

printf("filename :  %s\n",file);
printf("on device : major  %d/minor  %d  inode number :  %ld\n",major(statbuf.st_dev),minor(statbuf.st_dev),statbuf.st_ino);
printf("size   :%-10ld  type: %07o   permissions  :%05o\n",statbuf.st_size,statbuf.st_mode&S_IFMT,statbuf.st_mode&~(S_IFMT));
printf("owner  :%d    group:%d number of links:%d\n",statbuf.st_uid,statbuf.st_gid,statbuf.st_nlink);
printf("change time:%s\n",timeString(statbuf.st_ctime,timeBuf));
printf("modified time:%s\n",timeString(statbuf.st_mtime,timeBuf));
printf(" accesstime:%s\n",timeString(statbuf.st_atime,timeBuf));
return 0;
}
int main(int argc,const char ** argv)
{
int i;
int rc=0;
for(i=1;i<argc;i++)
{
rc|=statFile(argv[i]);
if ((argc-i)>1)
printf("\n");
}
return rc;
}

相关阅读 更多 +
排行榜 更多 +
哥布林弹球b服手游下载

哥布林弹球b服手游下载

休闲益智 下载
小马样式盒游戏下载

小马样式盒游戏下载

休闲益智 下载
异变小镇中文版下载安装

异变小镇中文版下载安装

冒险解谜 下载