Linxu文件相关操作函数

在这里插入图片描述

stat函数

头文件
 #include <sys/types.h>
 #include <sys/stat.h>
#include <unistd.h>
原型
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);//不追踪(不穿透)
功能:
包含文件的详细信息(所属用户、所属组、权限等)
参数:
pathname 文件路径
statbuf struct stat 类型结构体(上图有)
返回值
成功返回0,失败返回-1

struct stat 结构体

struct stat
{
    dev_t     st_dev;     /* ID of device containing file */文件使用的设备号ID
    ino_t     st_ino;     /* inode number */    索引节点号 
    mode_t    st_mode;    /* protection */  文件对应的模式,文件,目录等(下图)
    nlink_t   st_nlink;   /* number of hard links */    文件的硬连接数  
    uid_t     st_uid;     /* user ID of owner */    所有者用户识别号
    gid_t     st_gid;     /* group ID of owner */   组识别号  
    dev_t     st_rdev;    /* device ID (if special file) */ 设备文件的设备号
    off_t     st_size;    /* total size, in bytes */ 以字节为单位的文件容量   
    blksize_t st_blksize; /* blocksize for file system I/O */ 包含该文件的磁盘块的大小   
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */ 该文件所占的磁盘块  
    time_t    st_atime;   /* time of last access */ 最后一次访问该文件的时间   
    time_t    st_mtime;   /* time of last modification */ /最后一次修改该文件的时间   
    time_t    st_ctime;   /* time of last status change */ 最后一次改变该文件状态的时间 
};

st_mode使用

在这里插入图片描述

truncate函数

 头文件
 #include <unistd.h>
 #include <sys/types.h>
原型
int truncate(const char *path, off_t length);
 int ftruncate(int fd, off_t length);
功能:
truncate()会将参数path指定的文件大小改为参数length指定的大小。 
如果原来的文件大小比参数length大,则超过的部分会被删除。
返回值:
执行成功则返回0, 失败返回-1, 错误原因存于errno

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章