fwrite( ) in C | Detail

fwrite( ) function in C is basically used for writing a whole block of given stream(file).

fwrite function in c
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.

  1. fwrite( ) function in c
  2. header file for fwrite( ) function in c
  3. return value of fwrite( ) function in c
  4. fwrite( ) function in c syntax
  5. fwrite( ) function in c program

fread( ) function in C

The fwrite( ) function is nothing but, it is a standard library function of C and it also called file handling function which is basically used to write data and information in the whole block of the stream(file).

Header file for fwrite( ) function in C

This file function is define in <stdio.h> which is stand for standard input-output header file.

Return value of fwrite( ) function in C

On success fwrite( ) will write n items or total ( n * size) byte sto the file and will return n. On error and EOF of file it will return a number is less than n.

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:-

This function is basically used to write data and information in whole stream(file).

____________________________________

Post a Comment (0)
Previous Post Next Post