首页 > 其他 > 详细

struct stat结构体及其函数作用

时间:2019-10-09 13:41:12      阅读:81      评论:0      收藏:0      [点我收藏+]

stat结构体

stat结构体原型在<sys/stat.h>中因为有各种特殊数据类型,所以需<sys/types.h>头文件。struct stat  {   

    dev_t       st_dev;     /* ID of device containing file -文件所在设备的ID*/  
    ino_t       st_ino;     /* inode number -inode节点号*/    
    mode_t      st_mode;    /* protection -保护模式?*/    
    nlink_t     st_nlink;   /* number of hard links -链向此文件的连接数(硬连接)*/    
    uid_t       st_uid;     /* user ID of owner -user id*/    
    gid_t       st_gid;     /* group ID of owner - group id*/    
    dev_t       st_rdev;    /* device ID (if special file) -设备号,针对设备文件*/    
    off_t       st_size;    /* total size, in bytes -文件大小,字节为单位*/    
    blksize_t   st_blksize; /* blocksize for filesystem I/O -系统块的大小*/    
    blkcnt_t    st_blocks;  /* number of 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 - */    
};  
stat结构体定义了文件的各种属性数据。所以stat函数是用来把文件或者路径的属性信息识别并保存在内存中。

stat函数:
  
函数原型:

int stat(
  const char *filename    //文件或者文件夹的路径
  , struct stat *buf //获取的信息保存在内存中
);

该函数接受两个参数,第一个参数是文件名或者文件路径,第二个参数是stat结构体的指针或者地址。stat函数会把文件名的属性信息匹配stat结构体,并且保存在内存中。
函数执行成功返回0,失败返回-1,并把具体错误码放在errno中。
错误码详情:

EBADF: 文件描述词无效

EFAULT: 地址空间不可访问

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

ENAMETOOLONG:文件路径名太长

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

ENOMEM:内存不足

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

用法:

#include <time.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <stdio.h>  
#include <errno.h>  
  
int main( void )  
{  
   struct _stat buf;  
   int result;  
   char timebuf[26];  
   char* filename = "crt_stat.c";  
   errno_t err;  
  
   // Get data associated with "crt_stat.c":   
   result = _stat( filename, &buf );  
  
   // Check if statistics are valid:   
   if( result != 0 )  
   {  
      perror( "Problem getting information" );  
      switch (errno)  
      {  
         case ENOENT:  
           printf("File %s not found.\n", filename);  
           break;  
         case EINVAL:  
           printf("Invalid parameter to _stat.\n");  
           break;  
         default:  
           /* Should never be reached. */  
           printf("Unexpected error in _stat.\n");  
      }  
   }  
   else  
   {  
      // Output some of the statistics:   
      printf( "File size     : %ld\n", buf.st_size );  
      printf( "Drive         : %c:\n", buf.st_dev + ‘A‘ );  
      err = ctime_s(timebuf, 26, &buf.st_mtime);  
      if (err)  
      {  
         printf("Invalid arguments to ctime_s.");  
         exit(1);  
      }  
      printf( "Time modified : %s", timebuf );  
   }  
}  
输出大致如下:

File size            :732

Drive                 :C:

Time modified   :Thu Feb 07 14:39:36 2002

 11:58:15

 

struct stat结构体及其函数作用

原文:https://www.cnblogs.com/crine/p/11640908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!