C fwrite

Summary: in this tutorial, you will learn how to use the C fwrite funtion to create a random access file.

Introduction to the C fwrite() function

The fwrite() function is defined in the stdio.h library. The fwrite() function allows you to write data to a binary file.

Here’s the syntax of the fwrite() function:

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
Code language: C++ (cpp)

The fwrite() function writes a block of data in the memory to a file. It has the following parameters:

  • ptr is a pointer that points to the block of data in the memory to be written to the file. Typically, it’s a pointer to an array of data.
  • size specifies the number of bytes of each element that the function will write to the file.
  • count specifies the number of elements that the function will write to the file.
  • stream is the file pointer to the file to which the function will write.

The fwrite() function returns the number of elements that the function successfully writes to the file.

A simple C fwrite() function example

The following example uses the fwrite() function to write numbers from 0 to 9 to a file:

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

How it works.

First, open the file numbers.dat using the fopen() function. The fopen() uses the wb mode for writing binary data to a file. If the file doesn’t exist, it’ll create a new file. However, if the file already exists, it’ll overwrite the contents of the file.

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

Second, iterate from 0 to 9 and write each number to the file using the fwrite() function:

for (int i = 0; i < 10; i++) fwrite(&i, sizeof(int), 1, fp);
Code language: C++ (cpp)

The following statement writes an integer to the file:

fwrite(&i, sizeof(int), 1, fp);
Code language: C++ (cpp)

Third, close the file using the fclose() function:

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

A practical C fwrite() function example

Suppose that you need to write inventory data to a file. The inventory includes a list of record, where each record contains the following information:

  • Product name
  • Quantity

First, define a structure that includes the product name and quantity:

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

Second, define a function that allows users to enter a number of products:

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

The definition of the input() function is as follows:

bool input(product *product_list, const int size) { int i = 0; do { printf("Name:"); scanf("%s", &product_list[i].name); printf("Qty:"); scanf("%d", &product_list[i].qty); i++; } while (i < size); }
Code language: C++ (cpp)

Third, define a function that saves the records into the file specified by the filename:

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

The following shows the definition of the save() function:

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

The save() function does three things: open the file, write each strcture in the product list into the file, and close the it.

Finally, define a function that displays the entered inventory:

void display(product *product_list, const int size) ;
Code language: C++ (cpp)

The logic of the display() function is as follows:

void display(product *product_list, const int size) { // display the product list 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)

Put it all together:

#include <stdio.h> #include <stdbool.h> typedef struct { char name[40]; unsigned qty; } product; bool input(product *product_list, const int size); bool save(product *product_list, const int size, const char *filename); void display(product *product_list, const int size); int main() { const int SIZE = 10; product product_list[SIZE]; // input the product list input(product_list, SIZE); // save to the file save(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) { // display the product list 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 to the file bool save(product *product_list, const int size, const char *filename) { // write the inventory to the file FILE *fp = fopen(filename, "w"); if (fp == NULL) { puts("Error opening the file"); return false; } for (int i = 0; i < size; i++) fwrite(&product_list[i], sizeof(product), 1, fp); fclose(fp); return true; } // input inventory data bool input(product *product_list, const int size) { int i = 0; do { printf("Name:"); scanf("%s", &product_list[i].name); printf("Qty:"); scanf("%d", &product_list[i].qty); i++; } while (i < size); }
Code language: C++ (cpp)

Note that the SIZE constant defines the number of products that the users will enter per time.

Summary

  • Use the C fwrite() function to write a block of data in the memory into a file.
Was this tutorial helpful ?