趁現在有一點時間,來註記一下這個function是幹嘛的!!
功能說明:由檔案描述詞來取得檔案狀態。
標頭檔:#include<sys/stat.h>
#include<unistd.h>
函式宣告:int fstat(int filedes, struct stat *buffer);
函式說明:fstat()用來將引數filedes所指的檔案狀態,複製到引數buffer所指的結構中 (struct stat)。
回傳值:執行成功回傳0;失敗則回傳-1。
範例:
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define FILENAME "./TEXT.TXT"
#include <time.h>
int main(int argc, char *argv[])
{
struct stat stStatBuf;
int fd = 0;
memset(&stStatBuf, 0, sizeof(struct stat));
fd = open(FILENAME, O_RDONLY);
if(fd != -1) {
fstat(fd, &stStatBuf);
printf("st_dev = %c \n", stStatBuf.st_dev);
printf("st_ino = %d \n", stStatBuf.st_ino);
printf("st_mode = %d \n", stStatBuf.st_mode);
printf("st_nlink = %d \n", stStatBuf.st_nlink);
printf("st_uid = %d \n", stStatBuf.st_uid);
printf("st_gid = %d \n", stStatBuf.st_gid);
printf("st_rdev = %d \n", stStatBuf.st_rdev);
printf("st_size = %d \n", stStatBuf.st_size);
printf("st_blksize = %d \n", stStatBuf.st_blksize);
printf("st_blocks = %d \n", stStatBuf.st_blocks);
printf("st_atime= %s", ctime(&stStatBuf.st_atime));
printf("st_mtime = %s", ctime(&stStatBuf.st_mtime));
printf("st_ctime = %s", ctime(&stStatBuf.st_ctime));
}
close(fd);
return 0;
}
/*
struct stat {
dev_t st_dev; // ID of device containing file
ino_t st_ino; // inode 數量
mode_t st_mode; // 保護
nlink_t st_nlink; // hard links 數量
uid_t st_uid; // user ID 擁有者
gid_t st_gid; // group ID 擁有者
dev_t st_rdev; // device ID
off_t st_size; // 以byte為單位,計算大小
blksize_t st_blksize; // 系統I/O 區塊大小
blkcnt_t st_blocks; // 區塊取得數量
time_t st_atime; // 最後一次存取時間
time_t st_mtime; // 最後一次修改時間
time_t st_ctime; // 狀態修改時間
};
*/
執行結果及TEXT.TXT檔的內容:
參考資料:http://www.superfunction.net/C/system_call.jsp?transno=100000007
沒有留言:
張貼留言