Basic Input
and Output
IN C++ LANGUAGE
C++ uses a convenient
abstraction called streams
to perform input and
output operations in
sequential media such as
the screen, the keyboard or
a file. A stream is an entity
where a program can
either insert or extract
characters to/from.
istream
is for input stream
1
ostream
2 is for output stream
4 Classes of
Stream ifstream
3
is a special kind of input stream that reads
data from a data file.
4
ofstream
is a special kind of output stream that write file
out of a data file
Input Stream
Input Stream: If the direction
of flow of bytes is from the
device (for example,
Keyboard) to the main
memory then this process is
called input.
In most program
environments, the standard
input by default is the
keyboard, and the C++
stream object defined to
access it is cin.
Input Stream
Output Stream
If the direction of flow of bytes
is opposite, i.e. from main
memory to device( display
screen ) then this process is
called output.
On most program environments,
the standard output by default
is the screen, and the C++
stream object defined to access
it is cout.
Output Stream
GENERAL OVERVIEW
The C/C++ Standard Library
offers its users a variety of
functions, one of which is
header files.
Header for I/O
Header files offer these
features by importing them
into your program with the
help of a preprocessor
directive called #include.
These preprocessor directives
are responsible for instructing
the C/C++ compiler that these
files need to be processed
before compilation.
#INCLUDE<STDIO.H>
It is used to perform input and output
Standard operations using functions scanf() and
printf().
Header Files
And Their #INCLUDE<IOSTREAM>
Uses: It is used as a stream of Input and
Output using cin and cout.
#INCLUDE<STRING.H>
It is used to perform various
Standard functionalities related to string
manipulation like strlen(), strcmp(),
Header Files strcpy(), size(), etc.
And Their #INCLUDE<MATH.H>
Uses: It is used to perform mathematical
operations like sqrt(), log2(), pow(),
etc.
#INCLUDE<IOMANIP.H>
It is used to access set() and
Standard setprecision() function to limit the
decimal places in variables.
Header Files
And Their #INCLUDE<SIGNAL.H>
Uses: It is used to perform signal handling
functions like signal() and raise().
Escape
Sequence
Escape sequences are special
characters used in control string to
modify the format of an output.
An escape sequence consists of
two or more characters. For all
sequences, the first character will
be ”\” i.e. backslash. The other
characters determine the
interpretation of escape sequence.
\A
Alert Bell. This escape sequence is used
to play beep during execution.
Escape Example:
cout<<”COMPUTER\aPROGRAMMING”;
Sequence First of all, “COMPUTER” is printed then
a beep is played and after that
“PROGRAMMING” is printed.
\F
Form Feed. This escape sequence is used
in printing. It is used to insert a blank
paper in the printed output.
Escape Example:
cout<<”COMPUTER\fPROGRAMMING”;
Sequence After printing “COMPUTER”, the
computer will include a blank paper and
then print “PROGRAMMING”.
\N
New Line or Linefeed. When a new line
is necessary in the output, then this
escape sequence is used..
Escape Example:
cout<<”COMPUTER\nPROGRAMMING”;
Sequence First of all, “COMPUTER” is printed
and”\n” shifts the cursor to the next
line. Then “PROGRAMMING” is printed
on second line.
Output:
COMPUTER
PROGRAMMING.
\T
TAB. This escape sequence performs the
functionality of TAB key in the output
stream. The code given below will insert
Escape a TAB between two words
Example:
Sequence cout<<”COMPUTER\tPROGRAMMING”;
Output: COMPUTER PROGRAMMING
\B
Backspace. Whenever we want to delete
a single character, we press the button
“backspace” from our keyboard. The
Escape same functionality can be achieved in
C++ output with this escape sequence.
Sequence Example:
cout<<”COMPUTER\b PROGRAMMING”;
Output: COMPUTEPROGRAMMING
\R
Carriage Return. This escape sequence
moves the cursor at the beginning of
current line.
Escape Example:
Sequence cout<<”COMPUTER\rPROGRAMMING”;
Output: PROGRAMMINGER
Do you have any
questions of us?
Thank
You