共用方式為


_stat、_wstat 函式

取得有關檔案的狀態資訊。

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

參數

  • path
    out 包含現有檔案或目錄的路徑字串的指標。

  • buffer
    結構的指標存放區發生。

傳回值

如果檔案狀態取得的資訊,這些函式都會傳回 0。 傳回值為 1 表示錯誤,在這種情況下, errno 設定為 ENOENT,表示找不到檔案名稱或路徑。 EINVAL 的傳回值表示無效的參數; errno 在這個案例中將設為 EINVAL 。

注意事項注意事項

如果 path 包含目錄的位置,它不能包含一行後面的反斜線。如果存在,則會傳回 -1,並將 errno 設定為 ENOENT。

如需更多關於這些和其他回傳碼的資訊,請參閱 _doserrno 、 errno 、 _sys_errlist 和 _sys_nerr (_doserrno, errno, _sys_errlist, and _sys_nerr)

檔案的日期戳記如果在1970 年 1 月 1 日午夜之後且在 UTC 3000年12月31日 23:59:59 之前則能表示,除非您使用 _stat32 或 _wstat32,或定義日期只能表示到 UTC 2038年1月19日 03:14:07 情況的 _USE_32BIT_TIME_T。

備註

_stat 函式在結構取得 path 或目錄的指定資訊的檔案並儲存它所指向的 buffer。 _stat適時地自動處理多位元組字元的字串參數,根據目前使用的多位元組字碼頁來辨認多位元組字元序列。

_wstat 是 _stat 的寬字元版本。 _wstat 的 path 引數是寬字元字串。 _wstat 和 _stat 行為相同,除了 _wstat 不處理多位元組字元字串。

這些函式的變形支援 32或 64 位元的時間型別與 32 或 64 位元的檔案長度。 第一個數字後綴 (32 或 64) 表示使用的時間型別的大小,第二個後綴是 i32 或 i64 ,表示檔案大小為 32 位元或 64 位元的整數。

_stat 與 _stat64i32 等價,而 struct_stat包含 64 位元的時間。 除非定義 _USE_32BIT_TIME_T ,此時會是舊版的行為, _stat 使用 32 位元的時間,而 struct _stat 包含 32 位元的時間。 對 _stati64 而言也是如此。

注意事項注意事項

_wstat 不是使用 Windows Vista 符號連結一起使用。在這些情況下, _wstat 永遠報告檔案大小為 0。_stat 正確與符號連結一起使用。

這個函式會驗證它的參數。 如果path 或 buffer 任一為 NULL,就會叫用無效參數處理常式,如參數驗證中所述。

_stat 不同的時間型別和檔案長度型別

函式

是否定義 _USE_32BIT_TIME_T ?

時間型別

檔案長度型別

_stat, _wstat

未定義 _MBCS

64 位元

32 位元

_stat, _wstat

已定義

32 位元

32 位元

_stat32, _wstat32

不受巨集定義影響

32 位元

32 位元

_stat64, _wstat64

不受巨集定義影響

64 位元

64 位元

_stati64, _wstati64

未定義 _MBCS

64 位元

64 位元

_stati64, _wstati64

已定義

32 位元

64 位元

_stat32i64, _wstat32i64

不受巨集定義影響

32 位元

64 位元

_stat64i32, _wstat64i32

不受巨集定義影響

64 位元

32 位元

一般文字常式對應

TCHAR.H 常式

_UNICODE & _MBCS 未定義

_MBCS 已定義

_UNICODE 已定義

_tstat

_stat

_stat

_wstat

_tstat64

_stat64

_stat64

_wstat64

_tstati64

_stati64

_stati64

_wstati64

_tstat32i64

_stat32i64

_stat32i64

_wstat32i64

_tstat64i32

_stat64i32

_stat64i32

_wstat64i32

_stat 結構,定義在 SYS \ STAT.H,包括下列欄位。

  • st_gid
    擁有檔案群組的數值識別項 (Unix 專用) 這個欄位永遠為零在 Windows 系統。 為 Windows 檔案,已重新導向的檔案分類。

  • st_atime
    檔案存取權之前的時間。 在 NTFS,但不在 FAT 格式的驅動程式。

  • st_ctime
    檔案的建立時間。 在 NTFS,但不在 FAT 格式的驅動程式。

  • st_dev
    包含檔案的磁碟的提升數目與 st_rdev相同)。

  • st_ino
    資訊節點 ( inode) 的數字檔案 (Unix 專用)。 在 UNIX 檔案系統上, inode 描述檔案日期和時間戳記、使用權限和內容。 當檔案彼此時無法連接,它們共用相同的 inode。 inode和 st_ino,沒有在 FAT、HPFS 或 NTFS 檔案系統。

  • st_mode
    關於檔案模式的位元遮罩資訊。 如果 path 指定目錄, _S_IFDIR 位元設定;指定 path ,則為一般資料或裝置, _S_IFREG 位元設定。 使用者讀取/寫入位元根據檔案的使用權限模式設定;使用者執行位元根據副檔名設定。

  • st_mtime
    檔案的最後修改的時間。

  • st_nlink
    在非 NTFS 檔案系統永遠為 1 。

  • st_rdev
    包含檔案的磁碟的提升數目與 st_dev相同)。

  • st_size
    檔案的大小 (以位元組為單位);變化的 64 位元整數與 i64 後置字元**.**

  • st_uid
    擁有檔案使用者的數值識別項 (Unix 專用)。 這個欄位永遠為零在 Windows 系統。 為 Windows 檔案,已重新導向的檔案分類。

如果 path 參考裝置,則為 st_size,各種時間欄位,,則為 st_dev,且 _stat 結構的 st_rdev 欄位是無意義的。 由於 STAT.H 使用在 TYPES.H 定義的 _dev_t 型別,您必須在 STAT.H 之前包含 TYPES.H 在您的程式碼。

需求

常式

必要的標頭

選擇性的標頭檔

_stat, _stat32, _stat64, _stati64, _stat32i64, _stat64i32

<sys </stat.h> 遵循的 sys/types.h>

<errno.h>

_wstat, _wstat32, _wstat64, _wstati64, _wstat32i64, _wstat64i32

<sys/types.h 被 sys </stat.h 或 wchar.h 後面> > < >

<errno.h>

如需其他相容性資訊,請參閱<簡介>中的相容性

範例

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

.NET Framework 對等用法

請參閱

參考

檔案處理

_access、_waccess

_fstat、_fstat32、_fstat64、_fstati64、_fstat32i64、_fstat64i32

_getmbcp

_setmbcp