_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.\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位。因此該函數有很多變體,這些函數用法都是一樣的,我們根據具體情況選擇使用哪個函數。 [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 );
