admin 管理员组

文章数量: 1184232

c语言中获取文件属性(p87)

函数格式:

  • int stat(const char *filename , struct stat *buf); //对于软连接,穿透读取文件。
  • int fstat(int fp , struct stat buf); //先用open打开文件
  • int lstat(const char *filename , struct stat *buf) ; //对于软连接,不穿透读取文件,直接读取软链接文件。

功能:

获取指定文件的属性放入结构体buf中

  • 结构体stat定义如下图所示:

代码如下:

#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<stdlib.h>#include<time.h>#include<fcntl.h>intmain(){struct stat buf;int fst =stat("file2",&buf);if(fst ==-1){perror("tips message:");}printf("buf->st_dev = %ld\n",buf.st_dev);printf("buf->st_ino = %ld\n",buf.st_ino);printf("buf->st_mode = %d\n",buf.st_mode);printf("buf->st_nlink = %ld\n",buf.st_nlink);return0;}

本文标签: 对于软连 系统 编程