zoukankan      html  css  js  c++  java
  • struct stat结构体的详解和用法

    _stat函数用来获取指定路径的文件或者文件夹的信息。


    //
    ! 需要包含de头文件 #include <sys/types.h> #include <sys/stat.h> int stat(
      const char *filename //文件或者文件夹的路径
      , struct stat *buf //获取的信息保存在内存中
    ); //! prototype,原型

    正确——返回0

    错误——返回-1,具体错误码保存在errno

     

     

    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函数获取的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。
    #include <iostream>  
    #include <ctime>   
    #include <sys/types.h>    
    #include <sys/stat.h>     
    using namespace std;     
    int  main ()  
    {  
        struct stat buf;    
        int result;    
        result = stat ("./Makefile", &buf);    
        if (result != 0)  
          {  
              perror ("Failed ^_^");  
          }  
        else  
          {    
              //! 文件的大小,字节为单位    
              cout << "size of the file in bytes: " << buf.st_size << endl;    
              //! 文件创建的时间    
              cout << "time of creation of the file: " << ctime (&buf.st_ctime) << endl;   
              //! 最近一次修改的时间    
              cout << "time of last modification of the file: " << ctime (&buf.st_mtime) << endl;    
              //! 最近一次访问的时间    
              cout << "time of last access of the file: " << ctime (&buf.st_atime)<< endl;  
          }    
        return 0;    
    }  
    $ ./test  
      
    size of the file in bytes: 36  
      
    time of creation of the file: Sun May 24 18:38:10 2009  
      
    time of last modification of the file: Sun May 24 18:38:10 2009  
      
    time of last access of the file: Sun May 24 18:38:13 2009  
    c++获取文件信息——_stat函数的使用
     
    
    _stat函数的功能
    _stat函数用来获取指定路径的文件或者文件夹的信息。
    
    函数声明
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
    int _stat(  
       const char *path,  
       struct _stat *buffer   
    );  
    参数:
    
    path——文件或者文件夹的路径
    
    buffer——获取的信息保存在内存中
    
    返回值:
    
    正确——返回0
    
    错误——返回-1,具体错误码保存在errno中
    
    struct _stat结构体
    _stat结构体是文件(夹)信息的结构体,定义如下:
    
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
    struct stat {  
            _dev_t     st_dev;        //文件所在磁盘驱动器号  
            _ino_t     st_ino;        //inode,FAT、NTFS文件系统无意义  
            unsigned short st_mode;   //文件、文件夹的标志  
            short      st_nlink;      //非NTFS系统上通常为1  
            short      st_uid;        //UNIX系统上为userid,windows上为0  
            short      st_gid;        //UNIX系统上为groupid,windows上为0  
            _dev_t     st_rdev;       //驱动器号,与st_dev相同  
            _off_t     st_size;       //文件字节数  
            time_t st_atime;          //上次访问时间  
            time_t st_mtime;          //上次修改时间  
            time_t st_ctime;          //创建时间  
            };  
    
    以上信息就是可以通过_stat函数获取的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。
    
    例子
    注:该例子来自MSDN,http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx
    
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
    // crt_stat.c  
    // This program uses the _stat function to  
    // report information about the file named crt_stat.c.  
       
    #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.
    ", filename);  
               break;  
             case EINVAL:  
               printf("Invalid parameter to _stat.
    ");  
               break;  
             default:  
               /* Should never be reached. */  
               printf("Unexpected error in _stat.
    ");  
          }  
       }  
       else  
       {  
          // Output some of the statistics:   
          printf( "File size     : %ld
    ", buf.st_size );  
          printf( "Drive         : %c:
    ", 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
    
    支持不同时间长度和文件长度
    _stat函数中时间定义为64位,文件长度也定义为32位,同时使用char*表示文件名称。我们知道可以时间可以定义为64位和32位:__time64和__time32,文件名也可以用宽字符来表示,文件长度也可以定义为64位。因此该函数有很多变体,这些函数用法都是一样的,我们根据具体情况选择使用哪个函数。
    
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
    int _stat(  
       const char *path,  
       struct _stat *buffer   
    );  
    int _stat32(  
       const char *path,  
       struct __stat32 *buffer   
    );  
    int _stat64(  
       const char *path,  
       struct __stat64 *buffer   
    );  
    int _stati64(  
       const char *path,  
       struct _stati64 *buffer   
    );  
    int _stat32i64(str  
       const char *path,  
       struct _stat32i64 *buffer   
    );  
    int _stat64i32(str  
       const char *path,  
       struct _stat64i32 *buffer   
    );  
    int _wstat(  
       const wchar_t *path,  
       struct _stat *buffer   
    );  
    int _wstat32(  
       const wchar_t *path,  
       struct __stat32 *buffer   
    );  
    int _wstat64(  
       const wchar_t *path,  
       struct __stat64 *buffer   
    );  
    int _wstati64(  
       const wchar_t *path,  
       struct _stati64 *buffer   
    );  
    int _wstat32i64(  
       const wchar_t *path,  
       struct _stat32i64 *buffer   
    );  
    int _wstat64i32(  
       const wchar_t *path,  
       struct _stat64i32 *buffer   
    );  
     
  • 相关阅读:
    北京理工大学复试上机--2014
    北京理工大学复试上机--2013
    北京理工大学复试上机--2012
    北京理工大学复试上机--2011
    北京理工大学复试上机--2010
    Python的逻辑结构和函数
    Python的概述
    BOM
    正则表达式的匹配
    jQuery的插件和跨域、ajax
  • 原文地址:https://www.cnblogs.com/yaowen/p/4801541.html
Copyright © 2011-2022 走看看