Text And Binary Files | File Handling In C++ | C++ Tutorials

13 May 20120 comments

In C++ data can be stored in the files which can be of two types
  1. Text Files
  2. Binary Files
A text file is the one in which data is stored in the  form of ASCII character and is normally used for storing stream of characters with some special characters to mark end of each line. The source code files are themselves text files.

A binary file is 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. The word processor store documents as binary files.

The text and binary files can be distinguished as follows.

1)  In the text file, the character \n when written into a text file is actually converted into sequence of two characters \r and \n and then stored into this file. Similarly, when this sequence of characters \r and \n occur again while reading a file, then it is converted to a single character \n.

2)  In case of text file, the number of bytes required to store an integer value is proportional to its magnitude whereas in binary files the size is fixed, irrespective of its magnitude.

3) A text file contains characters which are readable and convey their exact meaning. The file can also be modified using any editor in a meaningful way. In contrast contents of binary file are not easily understood by its users and any modification made does not make any sense.

4) To open a binary file, you have to explicitly specify the ios::binary mode. If you  omit it then the file is opened as a text file by default.

5) The text files can easily be transferred from one computer system to another. On the other hand, binary files cannot be easily be transferred from one computer to another due to the variations in the internal representation which varies from computer to computer.
Share this article :

Post a Comment

 
Copyright © 2011. All Compiler - All Rights Reserved