fread( ) in C is basically used to read data from the whole block of the stream(file)
![]() |
| fread() in C |
Through this article we will be going to discuss fread( ) function of C in detail in which we will cover following topics.
- fread( ) function in c
- header file for fread( ) function in c
- return value of fread( ) function in c
- fread( ) function in c syntax
- fread( ) function in c program
fread( ) function in C
The fread( ) function is nothing but, it is a standard library function of C and it also called file handling function which is basically used to read data and information from the whole block of the stream(file).
Header file for ferror( ) function in C
This file function is define in <stdio.h> which is stand for standard input-output header file.
Return value of fread( ) function in C
On success it reads n items from the file and return n, if error or end of the file occur then it returns a value less than n. and we can use feof( ) and ferror( ) to check these conditions.
Syntax of fread( ) function in C
size_t fread(void *pointer,size_t size,size_t n,FILE *fptr):
Example of fread( ) function in C
#include<stdio.h>
#include<stdlib.h>
struct record
{
char name[20];
int roll;
float marks;
}student;
int main()
{
FILE *fp;
fp=fopen("stu.dat","rb");
if(fp==NULL)
{
printf("Error in opening file\n");
exit(1);
}
printf("\nNAME\tROLLNO\tMARKS\n");
while(fread(&student,sizeof(student),1,fp)==1)
{
printf("%s\t",student.name);
printf("%d\t",student.roll);
printf("%f\n",student.marks);
}
fclose(fp);
return 0;
}
Conclusion:-
fread( ) function is basically used to read whole data and information from the stream(file)
