zoukankan      html  css  js  c++  java
  • _stat函数的使用

    c++获取文件信息——_stat函数的使用

    _stat函数的功能

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

    函数声明

    [cpp] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. int _stat(  
    2.    const char *path,  
    3.    struct _stat *buffer   
    4. );  

    参数:

    path——文件或者文件夹的路径

    buffer——获取的信息保存在内存中

    返回值:

    正确——返回0

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

    struct _stat结构体

    _stat结构体是文件(夹)信息的结构体,定义如下:

    [cpp] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. struct stat {  
    2.         _dev_t     st_dev;        //文件所在磁盘驱动器号  
    3.         _ino_t     st_ino;        //inode,FAT、NTFS文件系统无意义  
    4.         unsigned short st_mode;   //文件、文件夹的标志  
    5.         short      st_nlink;      //非NTFS系统上通常为1  
    6.         short      st_uid;        //UNIX系统上为userid,windows上为0  
    7.         short      st_gid;        //UNIX系统上为groupid,windows上为0  
    8.         _dev_t     st_rdev;       //驱动器号,与st_dev相同  
    9.         _off_t     st_size;       //文件字节数  
    10.         time_t st_atime;          //上次访问时间  
    11.         time_t st_mtime;          //上次修改时间  
    12.         time_t st_ctime;          //创建时间  
    13.         };  


    以上信息就是可以通过_stat函数获取的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。

    例子

    注:该例子来自MSDN,http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx

    [cpp] view plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
    1. // crt_stat.c  
    2. // This program uses the _stat function to  
    3. // report information about the file named crt_stat.c.  
    4.    
    5. #include <time.h>  
    6. #include <sys/types.h>  
    7. #include <sys/stat.h>  
    8. #include <stdio.h>  
    9. #include <errno.h>  
    10.   
    11. int main( void )  
    12. {  
    13.    struct _stat buf;  
    14.    int result;  
    15.    char timebuf[26];  
    16.    char* filename = "crt_stat.c";  
    17.    errno_t err;  
    18.   
    19.    // Get data associated with "crt_stat.c":   
    20.    result = _stat( filename, &buf );  
    21.   
    22.    // Check if statistics are valid:   
    23.    if( result != 0 )  
    24.    {  
    25.       perror( "Problem getting information" );  
    26.       switch (errno)  
    27.       {  
    28.          case ENOENT:  
    29.            printf("File %s not found. ", filename);  
    30.            break;  
    31.          case EINVAL:  
    32.            printf("Invalid parameter to _stat. ");  
    33.            break;  
    34.          default:  
    35.            /* Should never be reached. */  
    36.            printf("Unexpected error in _stat. ");  
    37.       }  
    38.    }  
    39.    else  
    40.    {  
    41.       // Output some of the statistics:   
    42.       printf( "File size     : %ld ", buf.st_size );  
    43.       printf( "Drive         : %c: ", buf.st_dev + 'A' );  
    44.       err = ctime_s(timebuf, 26, &buf.st_mtime);  
    45.       if (err)  
    46.       {  
    47.          printf("Invalid arguments to ctime_s.");  
    48.          exit(1);  
    49.       }  
    50.       printf( "Time modified : %s", timebuf );  
    51.    }  
    52. }  

    输出大致如下:

    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 plain copy
     
     
     
     在CODE上查看代码片派生到我的代码片
      1. int _stat(  
      2.    const char *path,  
      3.    struct _stat *buffer   
      4. );  
      5. int _stat32(  
      6.    const char *path,  
      7.    struct __stat32 *buffer   
      8. );  
      9. int _stat64(  
      10.    const char *path,  
      11.    struct __stat64 *buffer   
      12. );  
      13. int _stati64(  
      14.    const char *path,  
      15.    struct _stati64 *buffer   
      16. );  
      17. int _stat32i64(str  
      18.    const char *path,  
      19.    struct _stat32i64 *buffer   
      20. );  
      21. int _stat64i32(str  
      22.    const char *path,  
      23.    struct _stat64i32 *buffer   
      24. );  
      25. int _wstat(  
      26.    const wchar_t *path,  
      27.    struct _stat *buffer   
      28. );  
      29. int _wstat32(  
      30.    const wchar_t *path,  
      31.    struct __stat32 *buffer   
      32. );  
      33. int _wstat64(  
      34.    const wchar_t *path,  
      35.    struct __stat64 *buffer   
      36. );  
      37. int _wstati64(  
      38.    const wchar_t *path,  
      39.    struct _stati64 *buffer   
      40. );  
      41. int _wstat32i64(  
      42.    const wchar_t *path,  
      43.    struct _stat32i64 *buffer   
      44. );  
      45. int _wstat64i32(  
      46.    const wchar_t *path,  
      47.    struct _stat64i32 *buffer   
      48. );  
  • 相关阅读:
    Spring MVC 学习总结(五)——校验与文件上传
    Spring MVC 学习总结(四)——视图与综合示例
    Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
    Spring MVC 学习总结(二)——控制器定义与@RequestMapping详解
    Spring学习总结(六)——Spring整合MyBatis完整示例
    Spring MVC 学习总结(一)——MVC概要与环境配置(IDea与Eclipse示例)
    Spring集成MyBatis完整示例
    Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
    Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一
    Android自动连接指定的wifi,免密码或指定密码
  • 原文地址:https://www.cnblogs.com/WAsbry/p/12950691.html
Copyright © 2011-2022 走看看