讀取 Linux 的檔案資訊
Linux 的檔案有很多資訊,即使只用到檔案大小也是很方便。
stat
-
struct stat
讀取檔案 (這裡讀 .gitignore)
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
int main()
{
struct stat st;
if(stat(".gitignore", &st) == -1){
return -1;
}
printf("I-node number: %ld\n", (long) st.st_ino);
printf("Mode: %lo (octal)\n", (unsigned long) st.st_mode);
printf("Link count: %ld\n", (long) st.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n", (long) st.st_uid, (long) st.st_gid);
printf("device containing file id: %ld\n", (long) st.st_dev);
printf("device id: %ld\n", (long) st.st_rdev);
printf("File size: %lld bytes\n", (long long) st.st_size);
printf("Preferred I/O block size: %ld bytes\n", (long) st.st_blksize);
printf("Blocks allocated: %lld\n", (long long) st.st_blocks);
printf("Last status change: %s", ctime(&st.st_ctime));
printf("Last file access: %s", ctime(&st.st_atime));
printf("Last file modification: %s", ctime(&st.st_mtime));
return 0;
}
I-node number: 20057552
Mode: 100664 (octal)
Link count: 1
Ownership: UID=1000 GID=1000
device containing file id: 66306
device id: 0
File size: 16 bytes
Preferred I/O block size: 4096 bytes
Blocks allocated: 8
Last status change: Tue Mar 29 14:03:01 2022
Last file access: Sun Oct 9 19:02:52 2022
Last file modification: Thu Jun 17 10:20:57 2021
參考資料 :
1.Linux manual page
2.程式人生
0 意見:
張貼留言