Open In App

Difference Between C++ Text File and Binary File

Improve
Improve
Like Article
Like
Save
Share
Report

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 file Binary 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.
10. Text files are used to store data more user friendly. Binary files are used to store data more compactly.
11. Mostly .txt and .rtf are used as extensions to text files. Can have any application defined extension.

C++




#include<iostream>                        
#include<fstream>                           
using namespace std;
int main() {
    fstream FileName;                      
    FileName.open("FileName.txt", ios::out);               
    if (!FileName) {                           
        cout<<" Error while creating the file ";         
    }
  
    else {
        cout<<"File created and data got written to file";   
        FileName<<"This is a blog posted on Great Learning"
        FileName.close() ;}
                     
    return 0 ;
    }


Output

File created and data got written to file

Code explanation :

1. The input/output stream is handled by the iostream library in this case.
2. Additionally, we have a library called fstream that manages files.
making an object of the fstream class with the name “FileName” on it.
3. Applying the open() function to the previously constructed object will allow us to create a new file with the mode set to “out,” allowing us to write data into the file.
4. To verify the creation of the file, we utilise the ‘if’ expression.
5. if the file is missing, prints the message to the terminal.
6. if the file is created or existing, prints the message to the console.
7. To close the file, we utilise the object’s close() function.



Last Updated : 19 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads