
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C Library - fread() function
The C library size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) function reads data from the given stream into the array pointed to, by ptr.It is commonly used for reading binary files but can be used for text files as well.
Syntax
Following is the C library syntax of the fread() function −
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
Parameters
This function accepts three parameters −
- ptr: A pointer to a block of memory where the read data will be stored.
- size: The size, in bytes, of each element to be read.
- nmemb: The number of elements, each of size bytes, to be read.
- stream: A pointer to the FILE object that specifies an input stream.
Return Value
The function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file (EOF) occurs. If size or nmemb is zero, fread returns zero and the contents of the memory pointed to by ptr are unchanged.
Example 1: Reading an Array of Integers from a Binary File
The program reads an array of 5 integers from a binary file and prints each integer.
Below is the illustration of C library fread() function.
#include <stdio.h> int main() { FILE *file; int numbers[5]; file = fopen("numbers.bin", "rb"); if (file == NULL) { perror("Error opening file"); return 1; } size_t result = fread(numbers, sizeof(int), 5, file); if (result != 5) { perror("Error reading file"); } else { for (int i = 0; i < 5; i++) { printf("Number %d: %d\n", i + 1, numbers[i]); } } fclose(file); return 0; }
Output
The above code produces following result−
Number 1: 1 Number 2: 2 Number 3: 3 Number 4: 4 Number 5: 5
Example 2: Reading a Structure from a Binary File
The program reads a structure Employee from a binary file and prints its members.
#include <stdio.h> typedef struct { int id; char name[20]; float salary; } Employee; int main() { FILE *file; Employee emp; file = fopen("employee.bin", "rb"); if (file == NULL) { perror("Error opening file"); return 1; } size_t result = fread(&emp, sizeof(Employee), 1, file); if (result != 1) { perror("Error reading file"); } else { printf("Employee ID: %d\n", emp.id); printf("Employee Name: %s\n", emp.name); printf("Employee Salary: %.2f\n", emp.salary); } fclose(file); return 0; }
Output
After execution of above code, we get the following result
Employee ID: 101 Employee Name: John Doe Employee Salary: 55000.00