C fread

Summary: in this tutorial, you’ll learn how to use the C fread() function to read data from a file into the memory.

Introduction to the C fread() function

The fread() function is defined in the stdio.h standard library. The fread() function reads data from a file into the memory. It has the following syntax:

size_t fread ( void * pstr, size_t size, size_t count, FILE * stream );
Code language: C++ (cpp)

The fread() function has the following parameters:

  • pstr is a pointer to a buffer where the fread() function will store the data.
  • size is the size of each element in bytes that the function will read.
  • count is the number of elements that the function will read from the file.
  • stream is a file pointer from which the function will read data.

The fread() function returns a number of elements that it successfully reads from the file. It’s equivalent to the count.

If the return value is less than count, there are two possible scenarios:

  • The function encounters the end-of-file.
  • Or an error occurred while reading the file.

If either size or count is zero, the fread() function returns zero.

C fread() function example

Suppose that you have the numbers.dat file that contains 10 integer numbers. The following example uses the fread() function to read all the numbers from the numbers.dat file.

Note that if you don’t have the numbers.dat file, you can run the program from the fwrite() function tutorial to create it.

#include <stdio.h> int main() { // open the file for writing binary data char *filename = "numbers.dat"; FILE *fp = fopen(filename, "rb"); if (fp == NULL) { printf("Error opening the file %s", filename); return -1; } // read numbers from the file int n; for (int i = 0; i < 10; i++) { fread(&n, sizeof(int), 1, fp); printf("%d ", n); } // close the file fclose(fp); return 0; }
Code language: C++ (cpp)

How it works.

First, open the numbers.dat file for reading binary data (mode rb)

char *filename = "numbers.dat"; FILE *fp = fopen(filename, "rb"); if (fp == NULL) { printf("Error opening the file %s", filename); return -1; }
Code language: C++ (cpp)

Second, use the fread() function to read each number at a time to the variable n and display it immediately to the screen:

int n; for (int i = 0; i < 10; i++) { fread(&n, sizeof(int), 1, fp); printf("%d ", n); }
Code language: C++ (cpp)

Third, close the file after reading:

// close the file fclose(fp);
Code language: C++ (cpp)

A practical C fread() function example

In the fwrite() function tutorial, you also learned how to write inventory data into the inventory.dat file. The inventory record has the following structure:

typedef struct { char name[40]; unsigned qty; } product;
Code language: C++ (cpp)

In this example, you’ll learn how to use the fread() function to read the inventory data and display it to the screen:

First, define the logic in the main() function of the program:

#include <stdio.h> #include <stdbool.h> typedef struct { char name[40]; unsigned qty; } product; int main() { // read the product const int SIZE = 2; product product_list[SIZE]; // save to the file read(product_list, SIZE, "inventory.dat"); // show the product list to the screen display(product_list, SIZE); return 0; }
Code language: C++ (cpp)

The program redefines the product structure:

typedef struct { char name[40]; unsigned qty; } product;
Code language: C++ (cpp)

It then calls the read() function to read data from the file to an array and invokes the display() function to show the inventory information.

Second, define the read() function that reads the inventory from a file to an array:

bool read(product *product_list, const int size, const char *filename);
Code language: C++ (cpp)

The following shows the read() function definition:

bool read(product *product_list, const int size, const char *filename) { FILE *fp = fopen(filename, "rb"); if (fp == NULL) { puts("Error opening the file"); return false; } for (int i = 0; i < size; i++) fread(&product_list[i], sizeof(product), 1, fp); fclose(fp); return true; }
Code language: C++ (cpp)

The read() function does three things: open the file, read data from the file into the product_list array, and close the file.

Third, define a function that displays the inventory data to the screen:

void display(product *product_list, const int size) { puts("Name\tQty"); puts("-------------"); for (int i = 0; i < size; i++) printf("%s \t %d\n", product_list[i].name, product_list[i].qty); }
Code language: C++ (cpp)

The following shows the complete program:

#include <stdio.h> #include <stdbool.h> typedef struct { char name[40]; unsigned qty; } product; bool read(product *product_list, const int size, const char *filename); void display(product *product_list, const int size); int main() { // read the product const int SIZE = 2; product product_list[SIZE]; // save to the file read(product_list, SIZE, "inventory.dat"); // show the product list to the screen display(product_list, SIZE); return 0; } void display(product *product_list, const int size) { puts("Name\tQty"); puts("-------------"); for (int i = 0; i < size; i++) printf("%s \t %d\n", product_list[i].name, product_list[i].qty); } // save product list ot the file bool read(product *product_list, const int size, const char *filename) { // write the inventory to the file FILE *fp = fopen(filename, "rb"); if (fp == NULL) { puts("Error opening the file"); return false; } for (int i = 0; i < size; i++) fread(&product_list[i], sizeof(product), 1, fp); fclose(fp); return true; }
Code language: C++ (cpp)

Summary

  • Use the C fread() function to read data from a file into the memory.
Was this tutorial helpful ?