Condividi tramite


fread_s

Legge i dati da un flusso. Questa versione di fread contiene miglioramenti della sicurezza, come descritto in Funzionalità di sicurezza in CRT.

size_t fread_s( 
   void *buffer,
   size_t bufferSize,
   size_t elementSize,
   size_t count,
   FILE *stream 
);

Parametri

  • buffer
    Percorso di archiviazione per i dati.

  • bufferSize
    Dimensione del buffer di destinazione in byte.

  • elementSize
    Dimensione dell'elemento da leggere in byte.

  • count
    Numero massimo di elementi da leggere.

  • stream
    Puntatore alla struttura FILE.

Valore restituito

fread_s restituisce il numero di (tutti) gli elementi che sono stati letti nel buffer, che può essere inferiore a count se avviene un errore di lettura o viene raggiunta la fine del file prima che count venga raggiunto. Utilizzare la funzione ferror o feof per distinguere un errore da uno stato di fine file. Se size o count è 0, fread_s restituisce 0 e il contenuto del buffer resta invariato. Se stream o buffer sono un puntatore null, fread_s richiama il gestore di parametro non valido, come descritto in Convalida dei parametri. Se l'esecuzione può continuare, la funzione imposta errno a EINVAL e restituisce 0 .

Per ulteriori informazioni sui codici di errore, vedere _doserrno, errno, _sys_errlist, and _sys_nerr.

Note

La funzione fread_s legge count oggetti di elementSize byte dall'input stream e li memorizza in buffer. Il puntatore del file associato allo stream (se presente) viene incrementato del numero di byte effettivamente letti. Se il flusso specificato viene aperto in modalità testo, le coppie di avanzamento riga di ritorno vengono sostituite con i singoli caratteri di avanzamento riga. La sostituzione non ha effetto sul puntatore del file o il valore restituito. La posizione del puntatore del file è indeterminata se si verifica un errore. Non può essere specificato il valore di un elemento parzialmente letto.

Questa funzione viene bloccata da altri thread. Se è richiesta una versione non bloccante, utilizzare _fread_nolock.

Requisiti

Funzione

Intestazione obbligatoria

fread_s

<stdio.h>

Per ulteriori informazioni sulla compatibilità, vedere Compatibilità.

Esempio

// crt_fread_s.c
// Command line: cl /EHsc /nologo /W4 crt_fread_s.c
//
// This program opens a file that's named FREAD.OUT and
// writes characters to the file. It then tries to open
// FREAD.OUT and read in characters by using fread_s. If the attempt succeeds,
// the program displays the number of actual items read.
 
#include <stdio.h>

#define BUFFERSIZE 30
#define DATASIZE 22
#define ELEMENTCOUNT 2
#define ELEMENTSIZE (DATASIZE/ELEMENTCOUNT)
#define FILENAME "FREAD.OUT"

int main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   for ( i = 0; i < DATASIZE; i++ )
      list[i] = (char)('z' - i);
   list[DATASIZE] = '\0'; // terminal null so we can print it
      
   // Open file in text mode:
   if( fopen_s( &stream, FILENAME, "w+t" ) == 0 )
   {
      // Write DATASIZE characters to stream 
      printf( "Contents of buffer before write/read:\n\t%s\n\n", list );
      numwritten = fwrite( list, sizeof( char ), DATASIZE, stream );
      printf( "Wrote %d items\n\n", numwritten );
      fclose( stream );
   } else {
      printf( "Problem opening the file\n" );
      return -1;
   }

   if( fopen_s( &stream, FILENAME, "r+t" ) == 0 )   {
      // Attempt to read in characters in 2 blocks of 11
      numread = fread_s( list, BUFFERSIZE, ELEMENTSIZE, ELEMENTCOUNT, stream );
      printf( "Number of %d-byte elements read = %d\n\n", ELEMENTSIZE, numread );
      printf( "Contents of buffer after write/read:\n\t%s\n", list );
      fclose( stream );
   } else {
      printf( "File could not be opened\n" );
      return -1;
   }
}
  
   
 
 

Vedere anche

Riferimenti

I/O di flusso

fwrite

_read