c语言中获取文件属性

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定义如下图所示:
    c语言中获取文件属性_第1张图片

代码如下:

#include
#include
#include
#include
#include
#include
#include

int main()
{

        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);
        return 0;
}

你可能感兴趣的:(笔记,c语言,c语言,linux)