c 獲取檔案資訊 stat函式的使用

2021-06-20 06:51:50 字數 2084 閱讀 9797

_stat函式用來獲取指定路徑的檔案或者資料夾的資訊。

int _stat(

const char *path,

struct _stat *buffer

);

引數:

path——檔案或者資料夾的路徑

buffer——獲取的資訊儲存在記憶體中

返回值:

正確——返回0

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

_stat結構體是檔案(夾)資訊的結構體,定義如下:

struct stat ;

以上資訊就是可以通過_stat函式獲取的所有相關資訊,一般情況下,我們關心檔案大小和建立時間、訪問時間、修改時間。

注:該例子來自msdn,

// crt_stat.c

// this program uses the _stat function to

// report information about the file named crt_stat.c.

#include #include #include #include #include int main( void )

}else

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

);

c 獲取檔案資訊 stat函式的使用

stat函式用來獲取指定路徑的檔案或者資料夾的資訊。cpp view plain copy int stat const char path,struct stat buffer 引數 path 檔案或者資料夾的路徑 buffer 獲取的資訊儲存在記憶體中 返回值 正確 返回0 錯誤 返回 1,具體...

使用stat函式獲取檔案基本資訊

函式原型 intstat constchar pathname,structstat buf 函式說明 給stat 函式傳遞乙個 pathname stat 函式返回乙個與此命名檔案有關的資訊結構,該資訊結構中包含檔案的基本資訊。include include include include inc...

C語言stat 函式 獲取檔案狀態

相關函式 fstat,lstat,chmod,chown,readlink,utime 標頭檔案 include include 定義函式 int stat const char file name,struct stat buf 函式說明 stat 用來將引數file name 所指的檔案狀態,複...