Functions of the _stat function
The _stat function is used to obtain information about a file or folder for a specified path.
function declaration
int _stat ( constChar *path, struct _stat *buffer);
Parameters:
path--the path to a file or folder
buffer--the information obtained is kept in memory
return value:
Correct--return 0
Error--return-1, specific error code saved in errno
struct _stat structural body
The _STAT structure is the structure of the file (clip) information, which is defined as follows:
structStat {_dev_t St_dev; //The disk drive letter where the file resides_ino_t St_ino;//inode,fat, NTFS file system meaninglessUnsigned ShortSt_mode;//flags for files and folders ShortSt_nlink;//typically 1 on a non-NTFS system ShortSt_uid;//0 on Unix system for Userid,windows ShortSt_gid;//0 on Unix system for Groupid,windows_dev_t St_rdev;//drive letter, same as St_dev_off_t st_size;//number of File bytestime_t St_atime;//Last access Timetime_t St_mtime;//Last Modified Timetime_t St_ctime;//creation Time};
The above information is all relevant information that can be obtained through the _stat function, in general, we care about file size and creation time, access time, modification time.
Example
Note: This example comes from 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>intMainvoid ){ struct_stat buf; intresult; Chartimebuf[ -]; Char* filename ="crt_stat.c"; errno_t err; //Get data associated with "crt_stat.c":result = _stat (filename, &buf); //Check If statistics is valid: if(Result! =0) {perror ("problem getting information" ); Switch(errno) { Caseenoent:printf ("File%s not found.\n", filename); Break; Caseeinval: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, -, &buf.st_mtime); if(Err) {printf ("Invalid arguments to ctime_s."); Exit (1); } printf ("Time modified:%s", TIMEBUF); }}
The output is roughly as follows:
File size:732
DRIVE:C:
Time Modified:thu Feb 07 14:39:36 2002
supports different time lengths and file lengths
The time in the _STAT function is defined as 64 bits, the file length is also defined as 32 bits, and the file name is represented with char*. We know that time can be defined as 64-bit and 32-bit: __time64 and __time32, filenames can also be expressed in wide characters, and file lengths can also be defined as 64 bits. So there are many variants of this function, and these functions are all the same, and we choose which function to use depending on the situation.
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 (strConst Char*Path,struct_STAT32I64 *buffer);int_stat64i32 (strConst Char*Path,struct_STAT64I32 *buffer);int_wstat (Constwchar_t *Path,struct_stat *buffer);int_wstat32 (Constwchar_t *Path,struct__stat32 *buffer);int_wstat64 (Constwchar_t *Path,struct__stat64 *buffer);int_wstati64 (Constwchar_t *Path,struct_STATI64 *buffer);int_wstat32i64 (Constwchar_t *Path,struct_STAT32I64 *buffer);int_WSTAT64I32 (Constwchar_t *Path,struct_STAT64I32 *buffer);
"Go" Windows C + + get file information use of the--_stat function