Function name : fwrite
Header file : <stdio.h>
Function : Write content to the stream, and add n data items to the given output stream stream starting from the pointer ptr. The length of each data item is size bytes.
Function prototype : int fwrite(void *ptr, int size, int nitems, FILE *stream);
Parameters : void *ptr Content to be written
int size The length of characters to be written
int nitems Number of characters to be written
FILE *stream file stream to be written
Return value : Returns the exact number of data items (not the number of bytes) on success, and returns a short count value on failure. may be 0
Program example: Write a structure to a file stream
#include<stdio.h>structmystruct{inti;charch;};intmain(void){FILE*stream=fopen(D:\test.txt,wb);structmystructs;if(!stream){fprintf(stderr,Cannotopenoutputfile . n);return1;}si=0;s.ch='A';if(fwrite(&s,sizeof(s),1,stream)==1){//Write the structure into the file stream printf (writetosuccessfuln);}else{printf(writetofailuren);}fclose(stream);return0;}
Running results
writetosuccessful