c++獲取文件信息——_stat函數的使用

c++獲取文件信息——_stat函數的使用

 

_stat函數的功能

_stat函數用來獲取指定路徑的文件或者文件夾的信息。

函數聲明

int _stat(
   const char *path,
   struct _stat *buffer 
);

參數:

path——文件或者文件夾的路徑

buffer——獲取的信息保存在內存中

返回值:

正確——返回0

錯誤——返回-1,具體錯誤碼保存在errno

struct _stat結構體

_stat結構體是文件(夾)信息的結構體,定義如下:

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

// 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.\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

支持不同時間長度和文件長度

_stat函數中時間定義爲64位,文件長度也定義爲32位,同時使用char*表示文件名稱。我們知道可以時間可以定義爲64位和32位:__time64和__time32,文件名也可以用寬字符來表示,文件長度也可以定義爲64位。因此該函數有很多變體,這些函數用法都是一樣的,我們根據具體情況選擇使用哪個函數。

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 
);

 

發佈了24 篇原創文章 · 獲贊 27 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章