Linux下获取文件或目录的状态信息

一.概述

     在Linux下进行文件、目录编程时经常需要获取指定文件的属性信息,比如: 文件类型、大小、创建日期、修改日期等属性信息。

     获取这些属性信息非常有用,比如: 打开一个设备文件时,为了防止未知错误,可以提前判断打开的设备文件是否是正确的类型。 拷贝文件时、读取文件时,mmap映射文件时,可以根据文件本身的大小,判断文件是否读写完毕。

二.获取方法

1.Linux下有3种函数可以快速得到文件、目录的属性信息:

stat 、fstat、lstat

2.API介绍

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int stat(const char *path, struct stat *buf);

函数功能: 获取文件或者目录的状态

函数参数:

const char *path  文件或者目录的路径.

struct stat *buf  保存获取的状态信息

int fstat(int fd, struct stat *buf);

函数功能: 获取文件的状态信息.

函数形参:

int fd   文件描述符

struct stat *buf  保存获取的状态信息

int lstat(const char *path, struct stat *buf);

函数功能: 获取文件的状态信息. 不区分链接文件.

保存状态信息的结构体:

struct stat {

   dev_t     st_dev;     /* ID of device containing file */

   ino_t     st_ino;     /* inode number */

   mode_t    st_mode;    /* protection  文件的类型*/

   nlink_t   st_nlink;   /* number of hard links */

   uid_t     st_uid;     /* user ID of owner */

   gid_t     st_gid;     /* group ID of owner */

   dev_t     st_rdev;    /* device ID (if special file) */

   off_t     st_size;    /* total size, in bytes  文件的字节大小*/

   blksize_t st_blksize; /* blocksize for file system I/O */

   blkcnt_t  st_blocks;  /* number of 512B blocks allocated */

   time_t    st_atime;   /* time of last access */

   time_t    st_mtime;   /* time of last modification */

   time_t    st_ctime;   /* time of last status change */

};

S_ISREG(m)  is it a regular file?  普通文件

S_ISDIR(m)  directory?  目录

S_ISCHR(m)  character device?  字符设备文件

S_ISBLK(m)  block device?  块设备文件

S_ISFIFO(m) FIFO (named pipe)? 管道文件

S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.) 链接文件

S_ISSOCK(m) socket? (Not in POSIX.1-1996.)  socket文件

在使用stat函数获取目录的信息时,st_size属性是没有的,目录的大小需要自己计算,里面可能包含了很多子目录等。

三.案例代码

下面使用state函数编写示例: 程序运行时,从命令行传入路径,调用opendir 函数打开目录,循环遍历目录,读取目录下的所有文件,并判断每个文件的类型,空间大小等信息打印到终端显示。

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <dirent.h>

#include <string.h>

#include <stdlib.h>

int main(int argc,char **argv)

{

    if(argc!=2)

    {

        printf("参数: ./a.out <目录的路径>\n");

        return 0;

    }

    

    /*1. 打开目录*/

    DIR *dir=opendir(argv[1]);

    if(dir==NULL)

    {

        printf("%s 目录打开失败.\n",argv[1]);

        return -1;

    }

    

    /*2. 遍历目录*/

    struct dirent *dir_info;

    struct stat s_buf; //存放状态信息的

    char *abs_path=NULL;

    while(dir_info=readdir(dir))

    {

        //A1. 申请空间

        abs_path=malloc(strlen(argv[1])+strlen(dir_info->d_name)+1);

        //A2. 拼接路径

        sprintf(abs_path,"%s%s",argv[1],dir_info->d_name);

        //A3. 获取文件的状态信息

        stat(abs_path,&s_buf);

        //A4. 打印文件的状态

        if(S_ISREG(s_buf.st_mode))

        {

            printf("%s 是普通文件.文件大小:%d Byte\n",abs_path,s_buf.st_size);

        }

        else if(S_ISDIR(s_buf.st_mode))

        {

            printf("%s 是目录.\n",abs_path);

        }

        else if(S_ISCHR(s_buf.st_mode))

        {

            printf("%s 字符设备文件.\n",abs_path);

        }

        else if(S_ISBLK(s_buf.st_mode))

        {

            printf("%s 块设备文件.\n",abs_path);

        }

        else if(S_ISFIFO(s_buf.st_mode))

        {

            printf("%s 是管道文件.\n",abs_path);

        }

        else if(S_ISLNK(s_buf.st_mode))

        {

            printf("%s 是链接文件.\n",abs_path);

        }

        //A5. 释放空间

        free(abs_path);

    }

    

    /*3. 关闭目录*/

    closedir(dir);

    return 0;

}

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI+程序员在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值