Aquí, vamos a aprender acerca de la función fwrite () de cabecera stdio.h biblioteca en lenguaje C con su sintaxis, ejemplo .
función
fwrite () en C
Prototipo:
size_t fwrite(void *buffer, size_t length, size_t count, FILE *filename);
Parámetros:
void *buffer, size_t length, size_t count, FILE *filename
Return escribir: size_t
Uso de la función:
El prototipo de la función fwrite () es:
size_t fwrite(void *buffer, size_t length, size_t count, FILE *filename);
En el manejo de archivos, a través de la función fwrite () escribimos número de recuento de objetos de tamaño longitud del nombre de archivo flujo de entrada de la matriz denominada como tampón. Sus devuelve el número de objetos que se va a escribir en el fichero. Si menor número de objetos será escrito o EOF se encuentra a continuación, se generará un error.
fwrite () ejemplo en C
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *f;
//initialize the arr1 with values
int arr1[5]={1,2,3,4,5};
int arr2[5];
int i=0;
//open the file for write operation
if((f=fopen("includehelp.txt","w"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//write the values on the file
if((fwrite(arr1,sizeof(int),5,f))!=5){
printf("File write error....n");
}
//close the file
fclose(f);
//open the file for read operation
if((f=fopen("includehelp.txt","r"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//read the values from the file and store it into the array
if((fread(arr2,sizeof(int),5,f))!=5){
printf("File write error....n");
}
fclose(f);
for(i=0;i<5;i++){
printf("%dn",arr2[i]);
}
return 0;
}
salida