Character Format and Binary Format in C++

Here we will discuss about character format and binary formatin C++,

before starting as we know that by using file handling, we store the data inputted by the user in a file.by doing this, we can create a large volume of database from which we access this data by re-executing the program. That is to say, the program behaves like a software.

Here we can store the data in the file by using two ways,
character form
Binary form

Here we will understand these forms from the difference between these two

  •  Data can be stored in character-form in two ways-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>   //  gets()
#include<fstream.h>

  void main()
  {
      clrscr();
      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();

      getch();
   }
OUTPUT:-
Enter Your Name : rahul singh
Name: rahul singh

The write() and read() functions are used for binary-form

here is the program ,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>

  void main()
  {
      clrscr();
      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();

      getch();
   }
OUTPUT:-
Enter Your Name : rahul singh
Name: rahul singh

don’t confuse both file are text type while stored data in it will be different format.

  • 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 –

character-format-in-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-format-in-cpp

  • Store data in character-form takes more memory than store data in binary-form

Like a name “rahul Singh” once stored in character-form

Here is the diagram,

size-of-character-format-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,

character-format-in-cpp

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

size-of-binary-format-cpp

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

binary-format-in-cpp

now you can see when we store same data, first in character-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 character-form.

and also we cannot read stored data in binary-form from backwards meaning stored data in binary-form remains secure such as,

binary-format-in-cpp

while in character-form we can easily read stored data from backside and such as,

character-format-in-cpp

also passed to different system while its not happen with binary-form.

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 marksheets here.

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

extra

from the above program below code used to store data in binary-form

      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-form;

Because the write() function automatically stores data in binary form. 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.h>
#include<conio.h>
#include<fstream.h>

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

   void example:: write(void)
    {
       clrscr();
       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
  void main()
   {
     clrscr();
  //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();

     getch();    //hold the screen

    clrscr();
    obj.read(); //displaying file content

    getch();
  }
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, now we will perform same task using ios::binary mode.

  • with ios::binary mode
#include<iostream.h>
#include<conio.h>
#include<fstream.h>

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

   void example:: write(void)
    {
       clrscr();
       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
  void main()
   {
     clrscr();
  //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();

     getch();    //hold the screen

    clrscr();
    obj.read(); //displaying file content

    getch();
  }
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 character-form will always be larger than the binary-form.

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