Share via


setbuf

Controlla il buffering del flusso. Questa funzione è deprecata. In alternativa, usare setvbuf .

Sintassi

void setbuf(
   FILE *stream,
   char *buffer
);

Parametri

stream
Puntatore alla struttura FILE .

buffer
Buffer allocato dall'utente.

Osservazioni:

La funzione setbuf controlla il buffering per stream. L'argomento stream deve fare riferimento a un file aperto che non è stato letto o scritto. Se l'argomento buffer è NULL, il flusso non viene memorizzato nel buffer. In caso contrario, il buffer deve puntare a una matrice di caratteri di lunghezza BUFSIZ, dove BUFSIZ è la dimensione del buffer, come definita in STDIO.H. Per il buffering di I/O viene usato il buffer specificato dall'utente, invece del buffer allocato dal sistema predefinito per il flusso specificato. Il stderr flusso non viene memorizzato nel buffer per impostazione predefinita, ma è possibile usare setbuf per assegnare buffer a stderr.

setbuf è stato sostituito da setvbuf, che è la routine preferita per il nuovo codice. A differenza setvbufdi , setbuf non ha modo di segnalare errori. setvbuf consente anche di controllare sia la modalità di memorizzazione nel buffer che le dimensioni del buffer. setbuf esiste per la compatibilità con il codice esistente.

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

Requisiti

Ciclo Intestazione obbligatoria
setbuf <stdio.h>

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

Esempio

// crt_setbuf.c
// compile with: /W3
// This program first opens files named DATA1 and
// DATA2. Then it uses setbuf to give DATA1 a user-assigned
// buffer and to change DATA2 so that it has no buffer.

#include <stdio.h>

int main( void )
{
   char buf[BUFSIZ];
   FILE *stream1, *stream2;

   fopen_s( &stream1, "data1", "a" );
   fopen_s( &stream2, "data2", "w" );

   if( (stream1 != NULL) && (stream2 != NULL) )
   {
      // "stream1" uses user-assigned buffer:
      setbuf( stream1, buf ); // C4996
      // Note: setbuf is deprecated; consider using setvbuf instead
      printf( "stream1 set to user-defined buffer at: %Fp\n", buf );

      // "stream2" is unbuffered
      setbuf( stream2, NULL ); // C4996
      printf( "stream2 buffering disabled\n" );
      _fcloseall();
   }
}
stream1 set to user-defined buffer at: 0012FCDC
stream2 buffering disabled

Vedi anche

I/O di flusso
fclose, _fcloseall
fflush
fopen, _wfopen
setvbuf