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 }