Skip to content
Related Articles

Related Articles

Difference Between C++ Text File and Binary File

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 May, 2022

A text file is the one in which data is stored in the form of ASCII characters and is normally used for storing a stream of characters. Text files are organized around lines, each of which ends with a newline character (‘\n’). The source code files are themselves text files.

A binary file is the one in which data is stored in the file in the same way as it is stored in the main memory for processing. It is stored in binary format instead of ASCII characters. It is normally used for storing numeric information (int, float, double). Normally a binary file can be created only from within a program and its contents can be read only by a program.

Text File vs Binary File

The following are some of the differences between text files and binary files.

S. No.Text fileBinary File
1.The text files can easily be transferred from one computer system to another. Binary files cannot easily be transferred from one computer system to another due to variations in the internal variations in the internal representation which varies from computer to computer.
2.It stores data using ASCII format i.e. human-readable graphic characters.It stores data in binary format i.e. with the help of 0 and 1.
3.These files are easily readable and modifiable because the content written in text files is human readable.
Content written in binary files is not human-readable and looks like encrypted content.
These files are not easily readable and modifiable because the content written in binary files is not human-readable and it is encrypted content.
4.These files create portability problems.These files are easily portable.
5.

Text files save the data by converting each digit in data into ASCII format which will take up much of the space as compared to the required one.

For example, the number 546378 is an integer that should occupy 4 bytes in the disk but it will occupy 6 bytes, 1 byte for each digit in the number.

These save memory because the data of any type will get stored in memory as per its memory size. 

For example, any integer number irrespective of individual digits in the number will be stored by consuming 4 bytes.

6.Any file is by default text file.The ios:: binary mode has to be used with binary files while opening them.
7.Error in a textual file can be easily recognized and eliminated.Error in a binary file corrupts the file and is not easily detected.
8. In a text file, a new line character is first converted to a carriage return-line feed combination and then written to the disk. Vice versa happens when a line is read from the text file.In binary file, no such conversion from newline to carriage return-line feed combination is done.
9.In a text file, a special character with ASCII code 26 is inserted at the end of the file. This character signals the EOF to the program when encountered.There is no such special character in the binary file to signal EOF.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!