Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The fread() function

The function fread() is used to read data from the binary file.

Syntax

size_type fread(void *p, size_type size, size_type number, File *fp);

That is

size_of_item fread(address_of_item, size_of_item, number_of_items, pointer_to_file);

*p – Address of memory where data/item present in a file is stored.
size – Size of data/item that will be read from the file.
number – Number of items that will be read from the file.
*fp – Points to the file from which data is read.

Example

#include<stdio.h>

struct Employee
{
    int id_no;
    char name[50];
    float salary;
};

struct Employee emp;

int main()
{
    FILE *fp;
    fp = fopen("D:\\C Programming\\file7","rb");
	
    if(fp==NULL)
    {
        printf("\n\n Unable to open the file");
        return 1;
    }
	
    fread(&emp,sizeof(struct Employee),1,fp);
    printf("\n ID NO = %d",emp.id_no);
    printf("\n Name = %s",emp.name);
    printf("\n Salary = %f",emp.salary);
    fclose(fp);
	
    return 0;
}

Output

C Programming Language fread Function in File Handling Example Output
 

Leave a reply