C fread

The target

In this article, you’ll learn how to use the C fread() function to read data from a file into memory.

Introduces the fread() function of C

The fread() function is defined in the stdio.h library. The read () function reads data from a file into memory. Its syntax is as follows:

size_t fread (void * pstr, size_t size, size_t count, FILE * stream);
Copy the code

The fread() function takes the following arguments:

  • PSTR is a pointer to the buffer where the fread() function will store data.
  • Size is the byte size of each element 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 reads data.

The getelement () function returns a number of elements that were successfully read from a file. It’s equal to count.

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

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

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

C example of fread() function

Suppose you have a file in numbers. Dat that contains 10 integers. 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 create it by running the program from the fwrite() function article.

#include <stdio.h> int main() {char *filename = "numbers. Dat "; FILE *fp = fopen(filename, "rb"); If (fp == NULL) {printf(" error opening %s file ", filename); return -1; } // Read int n from file; for (int i = 0; i < 10; i++) { fread(&n, sizeof(int), 1, fp); printf("%d ", n); } // Close the file fclose(fp); return 0; }Copy the code

How it is executed.

First, open the numbers.dat file to read binary data (mode RB)

char *filename = "number.dat"; FILE *fp = fopen(filename, "rb"); If (fp == NULL) = NULL {printf(" error opening file %s", filename); return 1; }Copy the code

Second, use the fread() function to read each number once into the variable n and immediately display it on the screen:

int n; for (int i = 0; i< 10; I++) {fread (&n, sizeof(int), 1, fp); Printf (" % d ", n); }Copy the code

Third, close the file after reading:

// Close the file fclose(fp);Copy the code

A practical example of the C fread() function

In the fwrite() function article, you also learned how to write inventory data to the inventory.dat file. The structure of the inventory record is as follows:

Typedef struct {char name[40]; unsigned qty; }product;Copy the code

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

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

#include <stdio.h> #include <stdbool.h> typedef struct { char name[40]; unsigned qty; } product; Int main() {// const int SIZE = 2; product product_list[SIZE]; // Save to file read(product_list, SIZE, "inventory.dat"); Display (product_list, SIZE); return 0; }Copy the code

The program redefines the product structure:

typedef struct
{
    char name[40];
    unsigned qty;
} product;
Copy the code

The read() function is then called to read data from the file into the array, and the display() function is called to display the inventory information.

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

bool read(product *product_list, const int size, const char *filename);
Copy the code

Here is the definition of the read() function:

bool read(product *product_list, const int size, const char *filename);
Copy the code

Here is the definition of the read() function:

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;
}
Copy the code

The read() function does three things: it opens the file, reads the data from the file into the product_list array, and closes the file.

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

Void display(product *product_list, const int size) {puts(" name \t number "); puts("-------------"); for (int i = 0; i < size; i++) printf("%s \t %d\n", product_list[i].name, product_list[i].qty); }Copy the code

The complete program is shown below:

#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() { const int SIZE = 2; product product_list[SIZE]; // Save to file read(product_list, SIZE, "inventory.dat"); // display(product_list, SIZE); return 0; } void display(product *product_list, const int size) {puts(" name \t number ");} void display(product *product_list, const int size) {puts(" name \t number "); puts("-------------"); for (int i = 0; i < size; i++) printf("%s \t %d\n", product_list[i].name, product_list[i].qty); Bool read(product *product_list, const int size, const char *filename) {FILE *fp = fopen(filename, "rb");} // Save to FILE bool read(product *product_list, const int size, const char *filename) {FILE *fp = fopen(filename, "rb"); If (fp == NULL) {puts(" error opening file "); return false; } for (int i = 0; i < size; i++) fread(&product_list[i], sizeof(product), 1, fp); fclose(fp); return true; }Copy the code

conclusion

  • Use the C fread() function to read data from a file into memory.