linux C/C++ struct stat示例

前言

struct stat这个结构体是用来描述一个linux系统文件系统中的文件属性的结构。stat函数获取文件的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。

struct stat 结构体介绍

首先还是先来所用到的struct stat结构体函数原型:

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

这些两个函数返回关于文件的信息。两个函数的第一个参数都是文件的路径,第二个参数是struct stat的指针。返回值为0,表示成功执行。
如果这些失败,则下面的信息设置为:

EBADF: 文件描述词无效

EFAULT: 地址空间不可访问

ELOOP: 遍历路径时遇到太多的符号连接

ENAMETOOLONG:文件路径名太长

ENOENT:路径名的部分组件不存在,或路径名是空字串

ENOMEM:内存不足

ENOTDIR:路径名的部分组件不是目录 

这两个方法区别在于stat没有处理字符链接(软链接)的能力,如果一个文件是符号链接,stat会直接返回它所指向的文件的属性;而lstat返回的就是这个符号链接的内容。

struct stat 信息如下:

struct stat {

        mode_t     st_mode;       //文件对应的模式,文件,目录等
        ino_t      st_ino;       //inode节点号
        dev_t      st_dev;        //设备号码
        dev_t      st_rdev;       //特殊设备号码
        nlink_t    st_nlink;      //文件的连接数
        uid_t      st_uid;        //文件所有者
        gid_t      st_gid;        //文件所有者对应的组
        off_t      st_size;       //普通文件,对应的文件字节数
        time_t     st_atime;      //文件最后被访问的时间
        time_t     st_mtime;      //文件内容最后被修改的时间
        time_t     st_ctime;      //文件状态改变时间
        blksize_t st_blksize;    //文件内容对应的块大小
        blkcnt_t   st_blocks;     //伟建内容对应的块数量
      };

定义以下POSIX宏来使用st_mode字段检查文件类型:

S_ISREG(m) 是常规文件吗?
S_ISDIR (m) 目录吗?
S_ISCHR (m) 字符设备?
S_ISBLK (m) 块设备?
S_ISFIFO(m)  FIFO(命名管道)?
S_ISLNK (m) 符号链接呢? (不是posix . 1的授权- 1996)。
S_ISSOCK (m) 套接字? (不是posix . 1的授权- 1996)

st_mode字段定义了以下标志:

文件类型位字段的S_IFMT 0170000 位掩码
S_IFSOCK 0140000 套接字
S_IFLNK 0120000 符号链接
S_IFREG 0100000 常规文件
S_IFBLK 0060000 块设备
S_IFDIR 0040000 目录
S_IFCHR 0020000 字符设备
S_IFIFO 0010000 先进先出
设置UID位S_ISUID 0004000
S_ISGID 0002000 集组id位(见下)
S_ISVTX 0001000 粘性位(见下)
S_IRWXU 00700 文件所有者权限掩码

struct stat 示例

下面的程序调用stat()并在返回的stat结构中显示所选字段。

stat.c

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

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


int main(int argc, char *argv[])
{

	   struct stat sb;

	   if (argc != 2) 
	   {
	       fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
	       exit(EXIT_FAILURE);
	   }

	   if (stat(argv[1], &sb) == -1) 
	   {
	       perror("stat");
	       exit(EXIT_SUCCESS);
	   }

	   printf("文件类型:                ");

	   switch (sb.st_mode & S_IFMT) //st_mode进行”&”操作,从而就可以得到某些特定的信息。
	   {
		   case S_IFBLK:  printf("块设备驱动\n");    
		   		break;
		   case S_IFCHR:  printf("字符设备\n");      
		   		break;
		   case S_IFDIR:  printf("目录\n");          
		   		break;
		   case S_IFIFO:  printf("先入先出/管道\n");  
		   		break;
		   case S_IFLNK:  printf("创建符号链接\n");                 
		   		break;
		   case S_IFREG:  printf("普通文件\n");            
		   		break;
		   case S_IFSOCK: printf("套接字\n");                  
		   		break;
		   default:       printf("未知数?\n");                
		   		break;
	   }

	   printf("索引节点号:            %ld\n", (long) sb.st_ino);

	   printf("Mode:                     %lo (octal)\n",(unsigned long) sb.st_mode);

	   printf("链接数:               %ld\n", (long) sb.st_nlink);
	   printf("所有权:                UID=%ld   GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);

	   printf("首选I/O块大小: %ld bytes\n",(long) sb.st_blksize);
	   printf("文件大小:                %lld bytes\n",(long long) sb.st_size);
	   printf("块分配:         %lld\n",(long long) sb.st_blocks);

	   printf("最后状态更改:       %s", ctime(&sb.st_ctime));
	   printf("最后的文件访问:         %s", ctime(&sb.st_atime));
	   printf("最后的文件修改:   %s", ctime(&sb.st_mtime));

	   exit(EXIT_SUCCESS);

}

编译结果:
在这里插入图片描述

接着咱在看看shell stat 命令打印的信息。跟stat.c函数做对比
在这里插入图片描述

可以看出stat.c打印的信息跟shell stat命令打印的信息都是一样的。

发布了71 篇原创文章 · 获赞 42 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chen1415886044/article/details/102887154