How to write fwrite() library function program in C programming (Example) ?
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[] = {10, 54, 21, 23, 54};
FILE *fp;
if((fp = fopen("input.txt", "wb")) == NULL)
{
printf("Error opening file.\n");
exit(1);
}
if(fwrite(a, sizeof a, 1, fp) != 1)
{
printf("Error writing to file\n");
exit(2);
}
fclose(fp);
return 0;
}