fwrite( ) function in C is basically used for writing a whole block of given stream(file).
![]() |
| fwrite() in C |
Through this article we will be going to discuss fwrite( ) function of C in detail in which we will cover following topics.
- fwrite( ) function in c
- header file for fwrite( ) function in c
- return value of fwrite( ) function in c
- fwrite( ) function in c syntax
- fwrite( ) function in c program
fread( ) function in C
Header file for fwrite( ) function in C
Return value of fwrite( ) function in C
Syntax of fwrite( ) function in C
size_t fwrite(const void *pointer, size_t size, size_t n, FILE *fptr):
Example of fwrite( ) function in C
#include<stdio.h>
#include<stdlib.h>
struct record
{
char name[20];
int roll;
float marks;
}student;
int main()
{
int i,n;
FILE *fp;
fp=fopen("stu.dat","wb");
if(fp==NULL)
{
printf("Error in opening file\n");
exit(1);
}
printf("Enter number of records:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name:");
scanf("%s",student.name);
printf("Enter roll no:");
scanf("%d",&student.roll);
printf("Enter marks:");
scanf("%f",&student.marks);
fwrite(&student,sizeof(student),1,fp);
}
fclose(fp);
return 0;
}
Conclusion:-
____________________________________
