fwrite

Sintaxis

#include <stdio.h>

size_t fwrite(void *buffer, size_t size, size_t number, FILE *file);

Descripción

Esta función escribe size*number caracteres desde buffer en file. El uso habitual es escribir en un fichero binario un struct o vector de structs. size establece el tamaño del struct a escribir y number el número de struct a escribir. La escritura hace que el cursor del fichero avance los bytes escritos.

Valor devuelto

El número de items escritos de tamaño size, o menos si ha ocurrido un error.

Portabilidad

ANSI, POSIX

Ejemplo

La función escribeDatoFicheroBinario escribe un registro en un fichero en la posición indicada como tercer parámetro. El primer parámetro es un puntero al descriptor del fichero y el segundo un puntero al registro a escribir.

#include <stdio.h>
#define LNOMBRE 50
#define LDIRECCION 100
typedef struct {
   char nombre[LNOMBRE];          
   char direccion[LDIRECCION];   
   long telefono;                
} TipoDato;
int escribeDatoFicheroBinario(FILE *fichero, TipoDato *dato, int pos){
   if(fseek(fichero,pos*sizeof(TipoDato),SEEK_SET)) 
      return 0;
   return fwrite(dato,sizeof(TipoDato),1,fichero)==1;
}