#include <stdio.h> size_t fwrite(void *buffer, size_t size, size_t number, FILE *file);
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.
El número de items escritos de tamaño size, o menos si ha ocurrido un error.
ANSI, POSIX
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; }