標籤:linux 資料 檔案系統 struct 結構
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct stat sb;
struct tm *ptr;
if (argc != 2)
{
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1)
{
perror("stat");
exit(EXIT_SUCCESS);
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino);
printf("Mode: %lo (octal)\n",(unsigned long) sb.st_mode);
printf("Link count: %ld\n", (long) sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
printf("Preferred I/O block size: %ld bytes\n",(long) sb.st_blksize);
printf("File size: %lld bytes\n",(long long) sb.st_size);
printf("Blocks allocated: %lld\n",(long long) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
}
通過這個結構體可以得到檔案的所有資訊
還有就是linux 檔案沒有建立檔案時間
linux 檔案包含三個時間
atime 最後訪問時間
ctime 最後更改屬性時間
mtime 最後修改時間
下面是結構體的詳細資料struct stat {
dev_t st_dev; //檔案的裝置編號
ino_t st_ino; //節點
mode_t st_mode; //檔案的類型和存取的許可權
nlink_t st_nlink; //連到該檔案的硬串連數目,剛建立的檔案值為1
uid_t st_uid; //使用者ID
gid_t st_gid; //組ID
dev_t st_rdev; //(裝置類型)若此檔案為裝置檔案,則為其裝置編號
off_t st_size; //檔案位元組數(檔案大小)
unsigned long st_blksize; //塊大小(檔案系統的I/O 緩衝區大小)
unsigned long st_blocks; //塊數
time_t st_atime; //最後一次訪問時間
time_t st_mtime; //最後一次修改時間
time_t st_ctime; //最後一次改變時間(指屬性)
};
man 2 stat
linux struct stat 檔案結構資訊