Text and Binary Files in C++ with Examples:
In this article, I am going to discuss Text and Binary Files in C++ with Examples. Please read our previous article where we discussed Serialization in C++ with Examples.
Text and Binary Files in C++:
There are two types of files:
- Text Files
- Binary Files
Let us understand the difference between them.
Difference between Text and Binary Files in C++ with Examples:
Text files are human-readable whereas binary files are machine-readable. Suppose we write a number that is 13. Then how it is written in the text file and binary file? Let’s see the difference.
13 is an integer value. The binary form of 13 is 1101. So how many bytes do integers take? In most compilers, an integer takes 4 bytes but to make our explanation easy, we consider an integer takes 2 bytes. So, suppose 13 (integer) is taking 2 bytes then how many bits are there in 1101?
0000 0000 0000 1101
Total 16 bits of a binary number. So, the same binary form is stored in the binary file with all 16 bits. That’s why we called this a binary file. Then what are text files? 13 will not be written in the text file. it will convert into ASCII. We know for every symbol, digit, or character there is some ASCII code available. The ASCII code of 1 is 49 and for 3 the ASCII code is 51. These are the ASCII codes for digits 1 and 3. Then what is the binary form of 49 and 51?
49 – 110001
51 – 110011
These are the binary form of 49 and 51. ASCII codes take 8 bits of binary so,
49 – 00110001
51 – 00110011
Now both have 8 bits of binary. We just added two zeroes at the starting to make these 8 bits of binary. So, what will be stored in the text file?
0011 0001 0011 0011 (This will be stored in the text file that is)
(ASCII code of 1) + (ASCII code of 3) = 49 + 51
= (binary code of 49) + (binary code of 51) = 0011 0001 0011 0011
This is how the bits are stored in the text file. For an integer, the digits will be converted into ASCII codes, then the ASCII will be converted into binary and then the 8 bits of binary will be stored in the text file. Both text and binary files stored the binary number. Then how text files are human-readable?
How text files are human-readable?
Suppose we have a text file that has stored 13. When we open this file in notepad then what will notepad do? For every 8 bits, it will be converted into ASCII and then display that symbol.
0011 0001 0011 0011
So, for this binary number, the first 8 bits will be converted into ASCII which is 1 and the next 8 bits will be converted into ASCII which is 3. So, 1 and 3 will be displayed in a notepad. And what about binary files? Suppose we have a binary file that contains the following code.
0000 0000 0000 1101
In a binary file, the first 8 bits will be taken. In this case, all the bits are 0, and 0 is the ASCII code of some unknown garbage symbol. It is not for the English alphabet or digits. We will get some boxes or question marks. So, we will get some junk characters. If you open this file in notepad then it might not show any meaningful symbols because the first 8 bits are not making any meaningful ASCII code. That’s it. We cannot read and understand it because that is a pure binary form or we can say it is machine-understandable.
The next important thing is if you are reading files in any programming language i.e. C or C++. In C++, if you are reading from the text file then you can use the insertion and extraction operator for reading and writing the data in the form of text. And if you want to read and write it in the form of binary then the first thing in C++ that you have to use is iso::binary mode. And also there are functions available for reading and writing that are read() and write(). read() is available in the file input stream and write() is available in the file output stream.
So, these functions you have to use for reading and writing the data in binary form. This is the difference between text files and binary files.
Which file is faster? Binary file or text file?
The binary file is faster than the text file. Because text file needs conversion (symbols to ASCII and ASCII to binary). But in the binary file, there is no conversion required.
Which file takes more space?
Text files will take more space and binary will take less space. Suppose we have the 4-digit number so a text file will take 4 bytes but a binary file will take 2 bytes.
The benefit of text files over binary files is we can read the text file. If we print text and binary file then we can understand the text file but we cannot understand binary file as it will contain junk characters. That’s all about the file handling.
In the next article, I am going to discuss Manipulators in C++ with Examples. Here, in this article, I try to explain Test and Binary Files in C++ with Examples and I hope you enjoy this Test and Binary Files in C++ with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this Working with Test and Binary Files in C++ with Examples article.