Last updated on July 27, 2020
Thefread() function is the complementary offwrite() function.fread() function is commonly used to read binary data. It accepts the same arguments asfwrite() function does. The syntax offread() function is as follows:
Syntax:size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
Theptr is the starting address of the memory block where data will be stored after reading from the file. The function readsn items from the file where each item occupies the number of bytes specified in the second argument. On success, it readsn items from the file and returnsn. On error or end of the file, it returns a number less thann.
Let's take some examples:
Example 1: Reading a float value from the file
123 | intval;fread(&val,sizeof(int),1,fp); |
This reads afloat value from the file and stores it in the variableval.
Example 2: Reading an array from the file
123 | intarr[10];fread(arr,sizeof(arr),1,fp); |
This reads an array of10 integers from the file and stores it in the variablearr.
Example 3: Reading the first 5 elements of an array
123 | intarr[10];fread(arr,sizeof(int),5,fp); |
This reads5 integers from the file and stores it in the variablearr.
Example 4: Reading the first 5 elements of an array
123 | intarr[10];fread(arr,sizeof(int),5,fp); |
This reads5 integers from the file and stores it in the variablearr.
Example 5: Reading the structure variable
1 2 3 4 5 6 7 8 910 | structstudent{charname[10];introll;floatmarks;};structstudentstudent_1;fread(&student_1,sizeof(student_1),1,fp); |
This reads the contents of a structure variable from the file and stores it in the variablestudent_1.
Example 6: Reading an array of structure
1 2 3 4 5 6 7 8 910 | structstudent{charname[10];introll;floatmarks;};structstudentarr_student[100];fread(&arr_student,sizeof(structstudent),10,fp); |
This reads first10 elements of type struct student from the file and stores them in the variablearr_student.
The following program demonstrates how we can usefread() function.
1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132333435 | #include<stdio.h>#include<stdlib.h>structemployee{charname[50];chardesignation[50];intage;floatsalary}emp;intmain(){FILE*fp;fp=fopen("employee.txt","rb");if(fp==NULL){printf("Error opening file\n");exit(1);}printf("Testing fread() function:\n\n");while(fread(&emp,sizeof(emp),1,fp)==1){printf("Name: %s\n",emp.name);printf("Designation: %s\n",emp.designation);printf("Age: %d\n",emp.age);printf("Salary: %.2f\n\n",emp.salary);}fclose(fp);return0;} |
Expected Output:
1 2 3 4 5 6 7 8 91011 | Testing fread() function:Name: BobDesignation: ManagerAge: 29Salary: 34000.00Name: JakeDesignation: DeveloperAge: 34Salary: 56000.00 |
How it works:
In lines 4-10, a structure employee is declared along with a variableemp . The structure employee has four members namely: name, designation, age and salary.
In line 14, a structure pointerfp of typestruct FILE is declared.
In line 15,fopen() function is called with two arguments namely"employee.txt" and"rb". On success, it returns a pointer to fileemployee.txt and opens the fileemployee.txt in read-only mode. On failure, it returnsNULL.
In lines 17-21, if statement is used to test the value offp. If it isNULL,printf() statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.
In lines 25-31, a while loop is used along withfread() to read the contents of the file. Thefread() function reads the records stored in the file one by one and stores it in the structure variableemp. Thefread() function will keep returning 1 until there are records in the file. As soon as the end of the file is encounteredfread() will return a value less than 1 and the condition in the while loop become false and the control comes out of the while loop.
In line 33,fclose() function is used to close the file.