C語言文件權限控制fstat

C語言fstat()函數:由文件描述詞取得文件狀態

相關函數:stat, lstat, chmod, chown, readlink, utime

頭文件:#include <sys/stat.h>   #include <unistd.h>

定義函數:int fstat(int fildes, struct stat *buf);

函數說明:fstat()用來將參數fildes 所指的文件狀態, 複製到參數buf 所指的結構中(struct stat). Fstat()與stat()作用完全相同, 不同處在於傳入的參數爲已打開的文件描述詞. 詳細內容請參考stat().

返回值:執行成功則返回0, 失敗返回-1, 錯誤代碼存於errno.

範例
#include <sys/stat.h>
#include <unistd.h>
#include <fcntk.h>
main()
{
   struct stat buf;
   int fd;
   fd = open("/etc/passwd", O_RDONLY);
   fstat(fd, &buf);
   printf("/etc/passwd file size +%d\n ", buf.st_size);
}
執行:
/etc/passwd file size = 705

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