File IO in C++ (text and binary files)

This C++ programming language tutorial will be in two parts. The first (this one) will cover the theory behind IO and in the second tutorial we will look at some examples.

Input-Output

Input – Output is a process of transfer of data from one computer device to another or from one part of the computer to another.

There are three categories of Input-Output:

  • Standard IO
  • Memory IO
  • Network IO

Standard IO is used frequently for which C++ provides cin, cout, cerr and clog streams. IO in C++ is done through stream classes, which are having the following inheritance hierarchy:

Streams inheritance hierarchy

We can use the ifstream, ofstream and fstream classes to perform file IO. (cin is an object of class istream and cout is an object of class ostream.)

File IO means transfer of data from secondary memory (hard disk) to main memory or vice-versa. A schematic showing the flow of data and classes involved is as follows:

Data flow

Note: The arrows indicate the flow of data.

Text and binary files

The C++ language supports two types of files:

  • Text files
  • Binary files

The basic difference between text files and binary files is that in text files various character translations are performed such as “\r+\f” is converted into “\n”, whereas in binary files no such translations are performed.

By default, C++ opens the files in text mode.

In the tables below we will see the various steps and operations that can (or must) be performed to use files in C++:

1)Creating or opening a
file

  • For writing data
Text Files

ofstream out (“myfile.txt”);

or

ofstream out;

out.open(“myfile.txt”);

Binary Files

ofstream out (“myfile.txt”,ios::binary);

or

ofstream out;

out.open(“myfile.txt”, ios::binary);

  • For Appending (adding text at the end of the existing file)
Text Files

ofstream out(“myfile.txt”,ios::app);

or

ofstream out;

out.open(“myfile.txt”, ios::app);

Binary Files

ofstream out
(“myfile.txt”,ios::app|ios::binary);

or

ofstream out;

out.open(“myfile.txt”, ios::app | ios::binary);

  • For reading data
Text Files

ifstream in (“myfile.txt”);

or

ifstream in ;

in.open(“myfile.txt”);

Binary Files

ifstream in (“myfile.txt”, ios::binary);

or

ifstream in ;

in.open(“myfile.txt”, ios::binary);

2) Closing Files (after reading or writing)

ofstream object
“out”
Ifstream object
“in”
out.close(); in.close();

3) Reading / Writing Data to and from files

Data Functions for reading
file
Function for writing
into file
char get(); put();
1
word
>>
(extraction
operator)
<< (insertion
operator)
>=1
word
getline(); << (insertion
operator)
Objects read() write()
Binary
data
Same as
above
Same as
above

4) Functions that can be used to perform special tasks

Operation function Description
Checking end of
file.
eof() Used to check eof during
the reading of file
Check if an operation
fails.
bad()

Returns true
if a reading or writing operation fails.

Check if an operation
fails.
Fail()

Returns true
in the same cases as bad(), but also in the case that a format error
happens.

Checking for opened
file.
is_open(); Checks if the file is
opened or not, returns true if the file is opened else
false
Number of bytes already
read.
gcount() Returns count of the
bytes read from the file
Ignoring characters
during file read.
ignore() Ignores n bytes from the
file. (get pointer is positioned after n
character)
Checking next
character.
peek() Checks the next available
character, will not increase the get pointer to next
character.
Random access (only for
binary files).
seekg()

seekp()

tellg()

tellp()

In case of binary files random access is performed using these functions. They either give or set the position of get and put pointers on the particular location

That is all for this C++ tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 4 responses to “File IO in C++ (text and binary files)”

Why not let us know what you think by adding your own comment!

  1. Keeran on June 3rd, 2012:

    Good Work..

  2. bob on October 29th, 2012:

    Thank you for making that page.

  3. Azam on November 6th, 2012:

    Thanks….this content will be very much help full for us.

  4. Anshul Mittal on November 26th, 2012:

    Very good work, the answer is to the point. It will help students to learn, understand and write better.