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

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