取得開啟之檔案的相關資訊。
語法
int _fstat(
int fd,
struct _stat *buffer
);
int _fstat32(
int fd,
struct _stat32 *buffer
);
int _fstat64(
int fd,
struct _stat64 *buffer
);
int _fstati64(
int fd,
struct _stati64 *buffer
);
int _fstat32i64(
int fd,
struct _stat32i64 *buffer
);
int _fstat64i32(
int fd,
struct _stat64i32 *buffer
);
參數
fd
已開啟之檔案的檔案描述項。
buffer
儲存結果的結構指標。
傳回值
如果取得檔案狀態資訊,則傳回 0。 -1 的傳回值表示錯誤。 如果檔案描述元無效或 buffer 為 NULL,則會叫用無效的參數處理常式,如參數驗證中所述。 如果允許繼續執行,在檔案描述元無效的情形中,errno 會設為 EBADF,如果 buffer 為 NULL,則會設為 EINVAL。
備註
_fstat 函式會取得與 fd 相關聯之開啟的檔案的相關資訊,並將它儲存在 buffer 結構中。 定義於 SYS\Stat.h 中,_stat 結構包含下列欄位。
| 欄位 | 意義 |
|---|---|
st_atime |
最後存取檔案的時間。 |
st_ctime |
建立檔案的時間。 |
st_dev |
如果是裝置,則為 fd,否則為 0。 |
st_mode |
檔案模式資訊的位元遮罩。 如果 fd 指的是裝置,則會設定 _S_IFCHR 位元。 如果 fd 指的是一般檔案,則會設定 _S_IFREG 位元。 讀取/寫入位元會根據檔案的權限模式設定。 _S_IFCHR 和其他常數於 SYS\Stat.h 中定義。 |
st_mtime |
檔案的上次修改時間。 |
st_nlink |
在非 NTFS 檔案系統上一律為 1。 |
st_rdev |
如果是裝置,則為 fd,否則為 0。 |
st_size |
檔案大小,以位元組為單位。 |
如果 fd 指的是裝置,則 st_atime、st_ctime、st_mtime和st_size 欄位並沒有意義。
因為 Stat.h 會使用 Types.h 中定義的 _dev_t 類型,因此您必須在程式碼中的 Stat.h 之前包含 Types.h。
使用 _stat64 結構的 _fstat64,允許表示至3000 年 12 月 31 日 23:59:59 UTC 為止的日期;而其他函式只能表示至 2038 年 1 月 18 日23:59:59 UTC 的日期。 1970 年 1 月 1 日午夜是所有這些函式的日期範圍下限。
這些函式的變化支援 32 位元或 64 位元時間類型,以及 32 位元或 64 位元檔案長度。 第一個數值後置字元 (32 或 64) 表示所使用的時間類型大小,第二個後置字元為 i32 或 i64,表示檔案大小是以 32 位元或 64 位元整數來表示。
除非 _USE_32BIT_TIME_T 已定義,否則 _fstat 等於 _fstat64i32,且 _stat 包含 64 位元時間。 定義 _USE_32BIT_TIME_T 時,_fstat 會使用 32 位元時間,且 _stat 會包含 32 位元時間。 對於 _fstati64也是如此。
根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
_stat 的時間類型和檔案長度類型變數
| 函式 | 已定義 _USE_32BIT_TIME_T 嗎? |
時間類型 | 檔案長度類型 |
|---|---|---|---|
_fstat |
未定義 | 64 位元 | 32 位元 |
_fstat |
已定義 | 32 位元 | 32 位元 |
_fstat32 |
不會受到巨集定義的影響 | 32 位元 | 32 位元 |
_fstat64 |
不會受到巨集定義的影響 | 64 位元 | 64 位元 |
_fstati64 |
未定義 | 64 位元 | 64 位元 |
_fstati64 |
已定義 | 32 位元 | 64 位元 |
_fstat32i64 |
不會受到巨集定義的影響 | 32 位元 | 64 位元 |
_fstat64i32 |
不會受到巨集定義的影響 | 64 位元 | 32 位元 |
需求
| 函式 | 必要的標頭 |
|---|---|
_fstat |
<sys/stat.h> 和 <sys/types.h> |
_fstat32 |
<sys/stat.h> 和 <sys/types.h> |
_fstat64 |
<sys/stat.h> 和 <sys/types.h> |
_fstati64 |
<sys/stat.h> 和 <sys/types.h> |
_fstat32i64 |
<sys/stat.h> 和 <sys/types.h> |
_fstat64i32 |
<sys/stat.h> 和 <sys/types.h> |
如需相容性詳細資訊,請參閱相容性。
範例
// crt_fstat.c
// This program uses _fstat to report
// the size of a file named F_STAT.OUT.
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <share.h>
int main( void )
{
struct _stat buf;
int fd, result;
char buffer[] = "A line to output";
char timebuf[26];
errno_t err;
_sopen_s( &fd,
"f_stat.out",
_O_CREAT | _O_WRONLY | _O_TRUNC,
_SH_DENYNO,
_S_IREAD | _S_IWRITE );
if( fd != -1 )
_write( fd, buffer, strlen( buffer ) );
// Get data associated with "fd":
result = _fstat( fd, &buf );
// Check if statistics are valid:
if( result != 0 )
{
if (errno == EBADF)
printf( "Bad file descriptor.\n" );
else if (errno == EINVAL)
printf( "Invalid argument to _fstat.\n" );
}
else
{
printf( "File size : %ld\n", buf.st_size );
err = ctime_s(timebuf, 26, &buf.st_mtime);
if (err)
{
printf("Invalid argument to ctime_s.");
exit(1);
}
printf( "Time modified : %s", timebuf );
}
_close( fd );
}
File size : 16
Time modified : Wed May 07 15:25:11 2003
另請參閱
檔案處理
_access??_waccess
_chmod??_wchmod
_filelength??_filelengthi64
_stat、_wstat 函式