基础
有一个struct叫做stat,用来存放文件的属性
分析
- 先需要一个缓冲区用于存放文件的属性信息
- 如果遇到错误交给perror处理
- 否则(文件的信息成功的输出到了缓冲区中)就把缓冲区中的size输出
源代码
这里以打开etc/passwd文件为例
1 #include <stdio.h>
2 #include <sys/stat.h>
3
4 int main(){
5 struct stat infobuf;
6 if(stat("/etc/passwd",&infobuf)== -1)
7 perror("/etc/passed");
8 else
9 printf("The size of /etc/passwd is %d\n",infobuf.st_size);
10 }
运行结果: