fwrite in C writes nmemb elements from the given array to the output stream. for each object fputc is called size times (count of bytes for a single element) and file position indicator for the stream is advanced by the number of characters written.
Below example ask the name from the user and store it in the buffer. After getting the name it writes the name in the created file using the fwrite function.
#include <stdio.h>
//Maximum size of the array
#define MAX_SIZE 32
int main()
{
//file pointer
FILE *fp = NULL;
char buffer[MAX_SIZE] = {0};
//Get input from the user
printf("Enter your Name = ");
fgets(buffer,MAX_SIZE,stdin);
//create the file
fp = fopen("aticleworld.txt", "w");
if(fp == NULL)
{
printf("Error in creating the file\n");
exit(1);
}
//Write the buffer in file
fwrite(buffer, sizeof(buffer[0]), MAX_SIZE, fp);
//close the file
fclose(fp);
printf("File has been created successfully\n");
return 0;
}
Below example ask the name from the user and store it in the buffer. After getting the name it writes the name in the created file using the fwrite function.
Output:
![](https://nerdutella.com/assets/dropzone/uploads/tinymce_images/questions/22392/answers/mceu_31180615311655934743451.png)
need an explanation for this answer? contact us directly to get an explanation for this answer