歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix基礎知識 >> Unix編程之int fstat(int fildes,struct stat *buf);

Unix編程之int fstat(int fildes,struct stat *buf);

日期:2017/3/3 15:23:31   编辑:Unix基礎知識

/*

* 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()作用完全相同,不同處在於傳入的參數為已打開的文件描述詞.

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

*/

/* 范例 */

#include <sys/stat.h>     
#include <unistd.h>     
#include <stdio.h>     
#include <sys/types.h>      
#include <sys/stat.h>     
#include <fcntl.h>     
        
int main(int argc, char *argv)     
{     
    struct stat buf;     
    int fd;     
    fd = open ( "/etc/passwd", O_RDONLY );     
    fstat ( fd, &buf );     
    printf ( "/etc/passwd file size: %d\n", buf.st_size );     
    return 0;     
}

/* 執行 /etc/passwd file size = 705 */

Copyright © Linux教程網 All Rights Reserved