Open In App

How to create Binary File from the existing Text File?

In this article, we will discuss how to create a binary file from the given text file. Before proceeding to the steps, let’s have an introduction of what are text files and binary files.

Text files: Text files store data in human-readable form and sequentially.



Binary files: Binary files store data in the form of bits/groups of bits (bytes) in sequence. These bits’ combinations can refer to custom data. Binary files can store multiple types of data, such as images, audio, text, etc.

Problem Statement: The task here is to read data from a text file and create a new binary file containing the same data in binary form.



Example: Suppose there are details of customers in a file named “Custdata.txt” and the task is to create a binary file named “Customerdb” by reading data from the text file “Custdata.txt”.

Let us assume that “Custdata.txt” has the following data-

ID Name Age
1 Annil 22
2 Ram 45
3 Golu 25

There is no requirement, as such, to store all values in a record in text format. The internal format can be used to directly store values. An option that is good and can be taken as a choice is to define a structure for our record.

struct Customers 
{
    int ID;
    char name[30];
    int age;
}

The individual elements of structure customers can now be accessed by creating a structure variable cs as:

  1. cs.ID.
  2. cs.name.
  3. cs.age.

Let us look at the methods that would be required to read the text file and write in the binary file. The function required for reading is fscanf() and for writing is fwrite().

Reading: fscanf() function is used to read the text file containing the customer data.

Syntax:

int fscanf(FILE* streamPtr, const char* formatPtr, …);
This function reads the data from file stream and stores the values into the respective variables.

Parameters of fscanf():

There is a need to include <cstdio> header file for using this function.

Writing: fwrite() function is used in the program to write the data read from the text file to the binary file in binary form. 

Syntax:

size_t fwrite(const void * bufPtr, size size, size_t count, FILE * ptr_OutputStream);
The fwrite() function writes “count” number of objects, where size of every object is “size” bytes, to the given output stream.

Parameters of write():

There is a need to include<cstdio> header file for using this function.

Algorithm: Below is the approach for the program for reading the text file and writing the content in the binary form to a binary file.

Below is the C++ program for the above approach:




// C++ program for the above approach
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
  
// Creating a structure for customers
struct Customers {
    int ID;
    char name[30];
    int age;
};
  
// Driver Code
int main()
{
    struct Customers cs;
    int rec_size;
    rec_size = sizeof(struct Customers);
    cout << "Size of record is: "
         << rec_size << endl;
  
    // Create File Pointers fp_input for
    // text file, fp_output for output
    // binary file
    FILE *fp_input, *fp_output;
  
    // Open input text file, output
    // binary file.
    // If we cannot open them, work
    // cannot be done, so return -1
    fp_input = fopen("custdata.txt", "r");
  
    if (fp_input == NULL) {
        cout << "Could not open input file"
             << endl;
        return -1;
    }
  
    fp_output = fopen("Customerdb", "wb");
  
    if (fp_output == NULL) {
        cout << "Could not open "
             << "output file" << endl;
        return -1;
    }
  
    // Read one line from input text file,
    // into 3 variables id, n & a
    int id, a;
    char n[30];
  
    // Count for keeping count of total
    // Customers Records
    int count = 0;
    cout << endl;
  
    // Read next line from input text
    // file until EOF is reached
    while (!feof(fp_input)) {
  
        // Reading the text file
        fscanf(fp_input, "%d %s %d ",
               &id, n, &a);
  
        // Increment the count by one
        // after reading a record
        count++;
  
        // Structure variable cs is set
        // to values to elements
        cs.ID = id, strcpy(cs.name, n), cs.age = a;
  
        // Write the structure variable
        // cs to output file
        fwrite(&cs, rec_size, 1, fp_output);
        printf(
            "Customer number %2d has"
            " data %5d %30s %3d \n",
            count, cs.ID,
            cs.name, cs.age);
    }
  
    cout << "Data  from file read"
         << " and printed\n";
    cout << "Database created for "
         << "Customers info\n";
  
    // Count contains count of total records
    cout << "Total records written: "
         << count << endl;
  
    // Close both the files
    fclose(fp_input);
    fclose(fp_output);
  
    return 0;
}

Output:

Size of record is: 40

Customer number  1 has data     1                    Annil  22
Customer number  2 has data     2                      Ram  45
Customer number  3 has data     3                    Golu  25          
Data  from file read and printed
Database created for Customers info     
Total records written: 3

Explanation: There were 3 records in the file “custdata.txt” that were read, printed, and saved in the “Customerdb” file in binary mode.
The text file contains 3 fields in every line- ID, Name, and Age.
The first line of the text file contains-

 1                    Annil  22

This line is read from the file, i.e, the data of ID, Name, and Age are read and their values are assigned to the variables id, n, and a respectively. Now assign the values of these 3 variables to the data members:  ID, name[], and age of the structure variable cs respectively.
Now using fwrite(&cs, rec_size, 1, fp_output), write one cs object of size rec_size  to the binary file- ” fp_output” (binary mode).
This procedure continues for all the lines of text files until the end of the file is reached. After the execution of the program ends, the file “custdata.txt” will remain as it is, as it was opened in the read mode-

 1                         Annil  22
 2                           Ram  45
 3                          Golu  25

This data is read line by line and written to the file “Customerdb” in binary mode. The file “Customerdb” will have the contents in binary form, which is not readable. The file, when opened normally, looks like this :


Article Tags :