linux编程之stat函数

来源:互联网 发布:淘宝代销利润如何算 编辑:程序博客网 时间:2024/06/15 21:02

stat函数用来获取文件的信息,如文件大小,修改时间等

stat函数的返回值是一个结构体其名字为struct stat

struct stat {  

dev_t st_dev;/* 包含这个文件的设备 ID */  

ino_t st_ino;/* inode 编号 */  

mode_t st_mode;/* 访问权限 */  

nlink_t st_nlink;/* 硬链接数量 */  

uid_t st_uid;/* 用户ID */  

gid_t st_gid;/* 组ID */  

dev_t st_rdev;/* 设备ID */  

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;/* 最后状态更改时间 */}

stat函数的原型是:

intstat(const char *pathname, struct stat*buf);

第一个参数是文件的路径名,第二个输出参数用来保存返回值。执行成功后返回0,否则失败,失败后改变全局变量errno的值。

使用示例:

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <stdio.h>

int main(int argc,char * argv[]){

struct stat st;

int f=stat(argv[1],&st);

if(f==0)

{

printf()/*打印相关参数*/

}

}



原创粉丝点击