Unit-6 Managing IO Formats and Operations
Unit-6 Managing IO Formats and Operations
Introduction:
Until now we are using Console Oriented Input/output System. So using Console Application, when a
Program is terminated, the Program data stored in main memory is lost. For Small Program like calculator,
we don't need to store data permanently. but what about the large programs?
Whenever we create a Program, and enter any data to process, after the program is terminated, the data
stored in the memory will lost. So to store data Permanently, C++ provide File handling.
File:
File is a collection of bytes that is stored on secondary storage devices like disk.
File Handling:
File Handling provides a mechanism to store output of a program in a file and read from a file on the disk.
So far, we have been using <iostream.h> header file which provide function cin / cout to take input from
console and write output to a console respectively. To understand C++ file operations like read and write,
we must first understand the concept of a stream in C++.
There are two types of streams: input streams and output streams.
An input stream is used to read the data from an external input device such as a keyboard, while an output
stream is used to write data to the external output device such as a monitor.
The “iostream.h” file contains all the required standard input/output stream classes in the C++
programming language.
In addition, “ifstream,” which stands for “input file stream,” is used to read a stream of data from a file,
and “ofstream,” which stands for “output file stream,” is used to write a stream of data to a file.
To read and write from a file we are using the standard C++ library called fstream.
Note: Include <iostream> and <fstream> header files in your program to do file Operations.
1
The fstream library
The fstream library provides C++ programmers with three classes for working with files. These classes
include:
1. ofstream– This class represents an output stream. It is used for creating files and writing information
to files.
2. ifstream– This class represents an input stream. It is used for reading information from data files.
3. fstream– This class generally represents a file stream. It comes with ofstream/ifstream capabilities.
This means it is capable of creating files, writing to files, reading from data files.
File Operations
Open a File
We can use fstream object to open a file for writing or reading purpose.
If we use ifstream object, we can open a file for reading purpose only.
If we use ofstream object, we can open a file for writing purpose only.
fstream fileobj;
fileobj.open(“filename.txt”,ios::out);
2
File Open Modes
3
Close a File
A File must be closed after completion of all the operations related to file.
When C++ program terminates, it automatically flushes all the streams, release all the allocated memory
and closes all the opened files.
But it is always a good practice that a programmer should close all the opened files before program
termination.
fileobj.close();
While doing C++ Programming, we write information to a file from our program using streams insertion
Operator << just as we use that operator to output information to the screen.
The only difference is that we use an ofstream or fstream object instead of the cout object. The text to be
written to the file should be enclosed within double-quotes.
Syntax:
fileobj<<“Text here”;
4
Write a program to append data in a file.
Once the data is in a file, we must be able to read that data. we must open the file in a read access mode.
There are several ways to read data. We can read character data one character at a time or one string at a
time. The choice depends on the format of the data. Files we open for read access must exist already, or
C++ gives an error. we can't read a file that does not exist.
open() returns zero if the file does not exist when we open it for read access.
To read the same information form a file with space and new line, use getline(fileobj, stringvariable);
However, instead of using the cin object, you use the ifstream/ fstream object.