一、函式原型
#include
#include
#include
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
二、引數
1、path :檔名或者目錄名
2、fd : 檔案描述符
3、 struct stat ;
st_mode :該變數佔 2 byte,共16位
(1)、掩碼的使用: st_mode & 掩碼
(2)、其他人許可權( 0-2 bit )
(a)、s_iroth 00004 讀許可權
(b)、s_iwoth 00002 寫許可權 掩碼:s_irwxo
(c)、s_ixoth 00001 執行許可權
(3)、所屬組許可權(3-5bit)
(a)、s_irwxg 00070 讀許可權
(b)、s_irgrp 00040 寫許可權 掩碼:s_rwxg
(c)、s_ixgrp 00010 執行許可權
(4)、檔案所有者許可權(6-8bit)
(a)、s_irusr 00400 讀許可權
(b)、s_iwusr 00200 寫許可權 掩碼:s_irwxu
(c)、s_ixusr 00100 執行許可權
(5)、檔案特權位(9-11bit)
(a)、 s_isuid 0004000 設定使用者id
(b)、 s_isgid 0002000 設定組id 檔案特權位很少用
(c)、 s_isvtx 0001000 設定黏住位
(6)、檔案型別(12-15bit)
(a) 、s_ifsock 0140000 socket(套接字)
(b) 、s_iflnk 0120000 symbolic link(符號鏈結--軟連線)
(c) 、s_ifreg 0100000 regular file(普通檔案)
(d)、 s_ifblk 0060000 block device(塊裝置) 掩碼:s_ifmt
(e) 、s_ifdir 0040000 directory(目錄)
(f) 、 s_ifchr 0020000 character device(字元裝置)
(g)、 s_ififo 0010000 fifo(管道)
三、返回值
以上三個獲取檔案屬性的函式 若成功,返回0;若失敗,返回 -1;
四、stat、lstat、fstat之間的區別
1、fstat 函式:系統呼叫的是乙個 」檔案描述符」,而另外兩個則直接接收「檔案路徑」。檔案描述符是我們用 open 系統呼叫後得到的,而檔案全路徑直接寫就可以了。
五、使用 stat() 函式實現乙個簡單的 ls -l shell 命令:
#include#include#include#include// 所有者資訊
#include// 所屬組資訊
#include#include#include#includeint main(int argc,char *argv)
struct stat st;
int i;
for( i = 1; ipw_name;
// 檔案所屬組
char *filegroup = getgrgid(st.st_gid)->gr_name;
// 檔案大小
int size = (int)st.st_size;
// 檔案修改時間
char *time = ctime(&st.st_mtime);
char mtime[512]="";
strncpy(mtime,time,strlen(time)-1);
// 儲存輸出資訊格式
char buf[1024]=;
// 把對應資訊按格式輸出到 buf 中
sprintf(buf,"%s %d %s %s %d %s %s",perms,nums,fileuser,filegroup,size,mtime,argv[i]);
// 列印 buf
printf("%s\n",buf);
// drwxrwxr-x 3 arrayli arrayli 4096 11月 13 23:19 day05
// -rw-r--r-- 1 arrayli arrayli 8980 11月 7 22:05 examples.desktop
Qt獲取檔案屬性
在qt中有qfileinfo類專門提供了獲取檔案資訊的各種介面,比如檔名稱,位置資訊,檔案的許可權,目錄,檔案或符號連線,檔案大小,建立時間,最後修改時等等,下面通過 來看一些具體的屬性獲取。ifndef mainwindow h define mainwindow h include qt beg...
獲取檔案屬性函式
表頭檔案 include 函式定義 int stat const char file name,struct stat buf 函式說明 通過檔名filename獲取檔案資訊,並儲存在buf所指的結構體stat中 返回值 執行成功則返回0,失敗返回 1,錯誤 存於errno 需要include er...
常用檔案屬性獲取
朱老師物聯網大講堂 學習筆記 檔案屬性,檔案的屬性資訊,只能被專用的api開啟看到,常用的api有,stat,fstat,lstat,同時stat也是乙個shell命令,其內部呼叫的也是stat,int stat const char path,struct stat buf int fstat i...