difference between text file and binary file in C++

File handling allows us to store user input in a file so that even after the program is terminated, the data entered by the user is not removed or erase. As happens in a simple program, we access that data by re-executing the program, it behaves like a real world application.

Here you can store user input in a file in two ways, you can call it data format.

We will understand these two with some examples –

Here we will discuss text format and binary format in C ++,

  • Text format/character mode
  • Binary format

Here we will understand these format from the difference between these two.

It has text format by default.

Generally, here are two ways to store a data in text format –

  1. insertion operator (<<) to write, extraction operator (>>) to read
  2. put() for write, get() for read

Here is an example of text format below –

#include<iostream>
#include<stdio.h>   //  gets()
#include<fstream>
using namespace std;

  int main()
  {
      char *name;

      ofstream fout;
      cout<<"Enter Your Name :";
      gets(name);

      cout<<"Name: "<<name;// displaying on monitor

      fout.open("text.txt");
      fout<<name;//writing in file
      fout.close();

return 0;
 }

OUTPUT

Enter Your Name : rahul singh
Name: rahul singh

For the binary format, write() and read(), which are predefined functions, are used –

here is the program,

#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;

 int main()
  {
      char *name;
      ofstream fout;

      cout<<"Enter Your Name :";
      gets(name);

      cout<<"Name: "<<name; //displaying on monitor

      fout.open("binary.txt",ios::binary);
      fout.write((char*)&name,sizeof(name));//writing in file
      fout.close();

 return 0;
  }

OUTPUT

Enter Your Name : rahul singh
Name: rahul singh

Note here that txt is a file extension in both programs while the format of the data stored in it will be different (text / binary).

If using ios :: binary with << such as,

fout.open("text.txt",ios::binary); 
fout<<name;

Even then it will store the data in text format.

And if we don’t use ios :: binary with write() such as,

fout.open("binary.txt"); 
fout.write((char*)&name,sizeof(name));

So it will store records in binary format only up to a limit, so write() function with ios::binary is mandatory for binary format.

An example of this is given at the end of the page.

  • In character-form, the form in which the user inputs the data is stored in the file in the same form.
    Suppose the user stores a name like “rahul singh”, then it will be stored in the file as follows –

stored in text mode-cpp

  • In binary-form, the data inputted by the user is stored in the file in binary-form. Suppose the user stores a name, then it will be stored in the file as follows –

binary-mode-in-cpp

  • store data in  text-form takes more memory than store data in binary-form –
    Like a name “rahul Singh” once stored in text format–

store in text-formatin-cpp

 

Because we know that the memory size of char is 1 byte, so 1 byte for each character (the space button will also take 1 byte) such as,

cpp-text-format

And now the same name “rahul Singh” is now stored in binary-form.

Here is the diagram

binary-format-example-in-cpp

Because the memory size of int is 2 bytes, therefore,

cpp-binary-format

now you can see when we store same data, first in text-format it takes 11 byte while storing in binary-form it takes only 2 byte.

  • since the size of store data in character-form is more than the size of store data in binary-form. Therefore data stored in binary-form is accessed faster than data stored in text-form.

Suppose if the user stores a name “rahul singh“, The data file stored in binary format gets converted into binary or some special character which cannot be read by humans.

binary-file -in-cpp

Because the data stored in the text format can be read from the backside even without a program which reduces security.

text-file-in-cpp

Now we know that stored data in binary-form is safer and faster than character-form.

But store data in character-form also has an advantage, such as printing student marksheet, print bill receipt etc. we use character-form to perform such tasks.

Let’s understand this with an example,

suppose a program is “Student Management System” which stores all the records of the students. in this we have stored the data in binary-form. But if there is an option to print the marksheet of the student then it can be a better program. So for this task we can use character-form.

Using the character-form, we have printed some mark sheets here.

It is not necessary that we can use character-form only to print a student mark sheet. It is also a better option for all those who need a physical output.

Extra

To store data in binary-format we use the following syntax –

But here we have not used ios :: binary but still it stores the data in binary format

This means that we are storing data in binary format in two ways.

      ofstream fout;
      fout.open("binary.txt");// here ios::binary not used
      fout.write((char*)&name,sizeof(name));
      fout.close();

Here we have not used ios::binary, yet it will store data in binary form.

meaning in first case

fout.open("binary.txt")
fout.write((char*)&name,sizeof(name));

And in second case

fout.open("binary.txt",ios::binary)
fout.write((char*)&name,sizeof(name));

Both will store data in binary-format.

Because the write() function automatically stores data in binary format. But now the question arises whether binary mode is optional? The answer is No.

So why is it not used in the first case. This is because ios::binary mandatory when we store a large volume numeric data.

let’s understand this with the below programs,

here both below program execute 15th times (means we write a single file 15th times) because we already said that ios::binary mandatory when we store large volume numeric data.

  • without ios::binary mode
#include<iostream>
#include<fstream>
using namespace std;

 class example
  {
   public:
     int number;
     void write();
     void read();
  };
 example obj;   // object created

   void example:: write(void)
    {
       ofstream fout;
       cout<<"Press any key to write file:";

	fout.open("myfile.txt",ios::app);
	fout.write((char*)&obj,sizeof(example));
	fout.close();
     }

    void example::read(void)
     {
	ifstream fin;
	fin.open("myfile.txt");

	while(fin.read((char*)&obj,sizeof(example)))
	 {
	    cout<<number<<endl;
	 }
	fin.close();
      }

//main program start here
  int main()
   {
  //write a file 15th time
     obj.number=1; obj.write();
     obj.number=2; obj.write();
     obj.number=3; obj.write();
     obj.number=4; obj.write();
     obj.number=5; obj.write();
     obj.number=6; obj.write();
     obj.number=7; obj.write();
     obj.number=8; obj.write();
     obj.number=9; obj.write();
     obj.number=10; obj.write();
     obj.number=11; obj.write();
     obj.number=12; obj.write();
     obj.number=13; obj.write();
     obj.number=14; obj.write();
     obj.number=15; obj.write();

   obj.read(); //displaying file content
 return 0;
 }

OUTPUT

Press any key to continue

1
2
3
4
5
6
7
8
9
10
11
12
13
9083
5432
32159

now we can see above output we store number 1-15 but its not happen and print garbage value after number 13-15, now we will perform same task using ios::binary mode.

  • with ios::binary mode
#include<iostream>
#include<fstream>
using namespace std;

 class example
  {
   public:
     int number;
     void write();
     void read();
  };
 example obj;   // object created

   void example:: write(void)
    {
       ofstream fout;
       cout<<"Press any key to write file:";

	fout.open("myfile.txt",ios::binary|ios::app);
	fout.write((char*)&obj,sizeof(example));
	fout.close();
     }

    void example::read(void)
     {
	ifstream fin;
	fin.open("myfile.txt",ios::binary);

	while(fin.read((char*)&obj,sizeof(example)))
	 {
	    cout<<number<<endl;
	 }
	fin.close();
      }

//main program start here
  int main()
   {
  //write a file 15th time
     obj.number=1; obj.write();
     obj.number=2; obj.write();
     obj.number=3; obj.write();
     obj.number=4; obj.write();
     obj.number=5; obj.write();
     obj.number=6; obj.write();
     obj.number=7; obj.write();
     obj.number=8; obj.write();
     obj.number=9; obj.write();
     obj.number=10; obj.write();
     obj.number=11; obj.write();
     obj.number=12; obj.write();
     obj.number=13; obj.write();
     obj.number=14; obj.write();
     obj.number=15; obj.write();

    obj.read(); //displaying file content

return 0;
  }

OUTPUT

Press any key to continue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

in the above output using binary mode file store complete record and give right output.

The concept of file handling is sensitive because data types can take different memory in a different system. But the size of the text format will always be larger than the binary-format.

if a large volume data is of numeric types, then ios::binary will be mandatory whereas it will be optional for data of text format.

⚠️: The programs given in this article are based on Windows 10, 64-bit OS, x64 based pro and executed on Turbo C++ 64bit version.

Related exercise

Like it?

Leave a Reply

Your email address will not be published.