Console I/O Operations Console I/O Operations
Console I/O Operations Console I/O Operations
cout.width(5);
1 2 4 3 6
cout<<124<<36;
cout.width(5);
1 2 4 3 6
cout<<124;
cout.width(3);
cout<<36;
fill() and precision()
cout.fill(‘#’); # # 1 2 4
cout.width(5);
cout<<124;
cout.precision(3);
cout<<6.7291; 6.729
setf(flags) and unsetf(flags)
• C++ defines some format flags for standard
input and output, which can be manipulated
with the flags(), setf(), and unsetf() functions.
For example,
• cout.setf(ios::left);
User-Defined Manipulators
ostream &manipulator_name(ostream &stream)
{
…………………………………..
// manipulator code
…………………………….
return stream;
}
stream object is a reference to the invoking
object.
ostream &tab(ostream &stream)
{
stream<<‘\t’; or cout<<‘\t’;
return stream;
}
User-defined manipulator tab is invoked
and cout stream object is passed by
reference.
File Handling
<iostream.h>
Istream ostream
Ifstream ofstream
<fstream.h>
• Ifstream: it contains open() function with
default input mode and inherits
get(),getline(),>>,read(),seekg(),tellg() member
functions from istream classes.
• Ofstream: contains open() function with
default output mode and inherits
<<,put(),seekp(),tellp() and write() member
functions from ostream classes.
• fstream: contains open() function with default
i/o and inherit the meber fns from ostream
and istream classes through iostream class.
Opening and closing a file
• ifstream inp_file; // an input file
stream //object for handling input files.
ifstream_obj.seekg(5);
input pointer
output pointer
o/p pointer
• seekg():- moves get pointer to a specified
location.
• seekp():- moves put pointer to a specified
location.
main()
{
ifstream obj;
obj.open(“file_name”);
while(!obj.eof)
{-------
--------------}
}
• Fail():- to check that a file has been opened for i/p or
o/p successfully,or there is an error.if it fails,it returns a
nonzero character.it returns TRUE if the operation has been
unsuccessful such as processing a file which is not opened.
main()
{
ifstream obj;
obj.open(“f_name”);
while(!obj.good())
{--------------
---------------
}
}
Synchronous Exceptions Handling
• Errors that occur at run time are known as
exceptions.
• Eg:
1)division by zero
2) accessing an element that is out of bound f an
array.
3)Unable to open a file
4)Running out of memory and many more.
Exception Handling
• There is another type of exception known as
asynchronous exception that deals error
associated with events like
• Disk I/O completion
• Mouse click
• Network msg arrivals.
For handling such exceptional situations,an error
handling mechanism knwn as exception
handling is provided.
In c++ ,exception handling is
designed to handle only
synchronized exceptions.
Exception Handling Mechanisms
• Whenever an exception occurs in a c++
program portions of a program that detects
the exception can inform that exception has
occurred by throwing it.
Exception Handler
On throwing an exception
The program control immediately stops the step-by-step execution of the code and