stat、lstat函數(十)
stat結構體
代碼:
1 /* 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <unistd.h> 5 6 int stat(const char *pathname, struct stat *statbuf); 7 作用:獲取一個文件相關的一些信息 8 參數: 9 - pathname:操作的文件的路徑 10 - statbuf:結構體變量,傳出參數,用於保存獲取到的文件的信息 11 返回值: 12 成功:返回0 13 失敗:返回-1 設置errno 14 15 int lstat(const char *pathname, struct stat *statbuf); 16 參數: 17 - pathname:操作的文件的路徑 18 - statbuf:結構體變量,傳出參數,用於保存獲取到的文件的信息 19 返回值: 20 成功:返回0 21 失敗:返回-1 設置errno 22 23 */ 24 25 #include <sys/types.h> 26 #include <sys/stat.h> 27 #include <unistd.h> 28 #include <stdio.h> 29 30 int main() { 31 32 struct stat statbuf; 33 34 int ret = stat("a.txt", &statbuf); 35 36 if(ret == -1) { 37 perror("stat"); 38 return -1; 39 } 40 41 printf("size: %ld\n", statbuf.st_size); 42 43 44 return 0; 45 }