_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32

获取有关打开文件的信息。

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,一旦无效文件描述符,或对 EINVAL,因此,如果 buffer 是 NULL。

备注

_fstat 功能在结构获取有关打开文件的信息与 fd 并将其存储指向由 buffer。 _stat 结构,定义在 SYS \Stat .h,包含以下字段。

  • 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
    始终 1 在非 NTFS 文件系统。

  • st_rdev
    如果设备, fd;否则为 0。

  • st_size
    文件的大小 (以字节为单位)。

如果 fd 引用计算机, st_atime、 st_ctime、 st_mtime和 st_size 字段不是有意义的。

由于 Stat.h 使用 _dev_t 类型,在 Types.h 中定义,您必须在 Stat.h 的前面包括 Types.h 在代码。

_fstat64,使用 __stat64 结构,这允许文件创建日期下午 23:59 表示: 59, 3000 年十二月 31 日,, UTC;而其他功能下午 03:14 只表示日期: 一月 07 日 19 日 2038 中, UTC。 午夜, 1970 年一月 1 日,是日期范围的下限所有这些功能。

这些功能的变体支持 32 位或 64 位时类型和 32 位或 64 位文件长度。 第一个数字后缀 (32 或 64) 指示所用的时间类型的大小;第二后缀是 i32 或 i64,指示文件大小是否表示为 32 位或 64 位整数。

_fstat 与 _fstat64i32是等效的,因此, struct_stat 包含一个 64 位时间。 为 true,除非 _USE_32BIT_TIME_T 定义,因此,在旧行为实际情况下为; _fstat 使用 32 位时间,因此, struct_stat 包含 32 位时间。 上述情况同样适用于 _fstati64。

超时 _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 );
}
  

.NET Framework 等效项

不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见 平台调用示例

请参见

参考

文件处理

_access, _waccess

_chmod, _wchmod

_filelength, _filelengthi64

_stat, _wstat功能