Condividi tramite


_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64_fstat64i32

Ottiene informazioni su un file aperto.

Sintassi

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

Parametri

fd
Descrittore di file del file aperto.

buffer
Puntatore alla struttura per l'archiviazione dei risultati.

Valore restituito

Restituisce 0 se si ottengono le informazioni sullo stato dei file. Il valore restituito -1 indica un errore. Se il descrittore di file non è valido o buffer è NULL, viene richiamato il gestore di parametri non validi, come descritto in Convalida dei parametri. Se l'esecuzione può continuare, errno è impostata su EBADF per un descrittore di file non valido o su EINVAL se buffer è NULL.

Osservazioni:

La funzione _fstat ottiene informazioni sul file aperto associato a fd e le archivia nella struttura a cui punta buffer. La _stat struttura, definita in SYS\Stat.h, contiene i campi seguenti.

Campo Significato
st_atime Ora dell'ultimo accesso al file.
st_ctime Ora di creazione del file.
st_dev Nel caso di un dispositivo fd; in caso contrario, 0.
st_mode Maschera di bit per informazioni sulla modalità di file. Viene impostato il bit _S_IFCHR se fd fa riferimento a un dispositivo. Viene impostato il bit _S_IFREG se fd fa riferimento a un file ordinario. I bit di lettura/scrittura vengono impostati in base alla modalità di autorizzazione del file. _S_IFCHR e altre costanti sono definite in SYS\Stat.h.
st_mtime Ora dell'ultima modifica del file.
st_nlink Sempre 1 nel file system non NTFS.
st_rdev Nel caso di un dispositivo fd; in caso contrario, 0.
st_size Dimensioni del file, in byte.

Se fd fa riferimento a un dispositivo, i st_atimecampi , st_ctimest_mtime, e st_size non sono significativi.

Poiché Stat.h usa il _dev_t tipo , definito in Types.h, è necessario includere Types.h prima Stat.h nel codice.

_fstat64, che usa la struttura _stat64, consente di esprimere le date di creazione di file fino alle 23.59.59 del 31 dicembre 3000 UTC, mentre le altre funzioni rappresentano solo le date fino alle 23.59.59 del 18 gennaio 2038 UTC. Il limite inferiore dell'intervallo di date per tutte queste funzioni è Mezzanotte, 1 gennaio 1970.

Le variazioni di queste funzioni supportano tipi time a 32 o 64 bit e lunghezze di file a 32 o a 64 bit. Il primo suffisso numerico (32 o 64) indica le dimensioni del tipo time usato; il secondo suffisso è i32 o i64, che indica se le dimensioni del file sono rappresentate come intero a 32 bit o 64 bit.

A meno che _USE_32BIT_TIME_T non sia definito, _fstat è equivalente a _fstat64i32e _stat contiene un tempo a 64 bit. Quando _USE_32BIT_TIME_T viene definito, _fstat usa un'ora a 32 bit e _stat contiene un tempo a 32 bit. Lo stesso vale per _fstati64.

Per impostazione predefinita, lo stato globale di questa funzione è limitato all'applicazione. Per modificare questo comportamento, vedere Stato globale in CRT.

Variazioni del tipo di tempo e del tipo di lunghezza del file _stat

Funzioni _USE_32BIT_TIME_T definito? Tipo Time Tipo lunghezza file
_fstat Non definito 64 bit 32 bit
_fstat Definito 32 bit 32 bit
_fstat32 Non interessato dalla definizione macro 32 bit 32 bit
_fstat64 Non interessato dalla definizione macro 64 bit 64 bit
_fstati64 Non definito 64 bit 64 bit
_fstati64 Definito 32 bit 64 bit
_fstat32i64 Non interessato dalla definizione macro 32 bit 64 bit
_fstat64i32 Non interessato dalla definizione macro 64 bit 32 bit

Requisiti

Funzione Intestazione obbligatoria
_fstat <sys/stat.h> e <sys/types.h>
_fstat32 <sys/stat.h> e <sys/types.h>
_fstat64 <sys/stat.h> e <sys/types.h>
_fstati64 <sys/stat.h> e <sys/types.h>
_fstat32i64 <sys/stat.h> e <sys/types.h>
_fstat64i32 <sys/stat.h> e <sys/types.h>

Per altre informazioni sulla compatibilità, vedere Compatibility (Compatibilità).

Esempio

// 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

Vedi anche

Gestione dei file
_access, _waccess
_chmod, _wchmod
_filelength, _filelengthi64
_stat, _wstat funzioni