3. Contents
History of stream
Stream
Basics of input/output
Input/output operation
Keywords in C++
Standard output stream
Standard input stream
Un-buffered error stream(cerr)
Buffered error stream(clog)
Benefits
Conclusion
4. History of stream
Bjarne stroustarp, the creator of C++, wrote the first version of the stream I/O
library in 1984.
The library has undergone a number of enhancements.
Since this early version, including the intro of manipulators to control
formatting, and templatization to allow its use with charcter types other than
char.
Standardization in 1998 saw the library moved into the std namespace, and
the main header changed from <iostream.h> to <iostream>.
It is this standardized version that is converted in the rest
5. Introduction of stream
A stream is a flow of data from a program to a backing store, or from a
backing store to a program.
A stream is abstraction that represent a device on which input and output
operations are performed
Input and output is performed in the form of sequence of byte known as
stream.
A stream can represent many different kinds of sources and destinations,
including disk files ,devices other programs, and memory arrays.
6. Basic input/output in C++
C++ comes with libraries provide us with many ways for
performing input and output.
Input stream: If the direction of flow of byte is from device to
memory then it is called input.
Output stream: If the direction of flow of byte is opposite then it
is called output.
7. I/O stream library
<iostream>
iostream stands for standard input-output stream. This header file contains
definitions to objects like cin, cout, cerr etc.
<iomanip>
iomanip stands for input output manipulators.
The method is used for manipulating streams.
<fstream>
This header file mainly describe the file stream.
It is used to handle the data being read from a file as input or data being
written into file as output.
8. I/O stream library
<strstream>:
To summarize , stringstream is the convenient way to
manipulate string.
Stringstream is basically allows you to treats a string object
like a stream and use all stream functions and operates on it
<stdiostream>:
Contains information for program that mixes the C and C++ styles of I/O.
9. Keywords in C++
There are two keywords in C++. These are as follows;
Cout:
It is used very often for printing outputs. It is standard output stream.
Cin:
It is used often taking inputs respectively. It is standard input stream.
These two methods are basic methods of taking inputs and printing output in
C++.
To use cin and cout in C++ one must include the header file iostream in the
program.
10. Standard output stream(Cout)
Usually the standard output device is the display screen.
The C++ cout statement is the instance of the ostream class.
It is used to produce output on the standard output device, e.g. display screen.
The data is displayed bu using the insertion operator(<<).
#include <iostream>
using namespace std;
int main( )
{
char sample[ ] = “Geeks for Geeks”;
cout << sample << “ – A computer science portal for geeks”;
return 0;
}
Output:
Geeks for Geeks - A computer science portal for geeks.
11. Standard input stream(Cin)
Usually the standard input device in computer is the keyboard.
The C++ cin statement is the instance of the class istream.
It is used to read inputs from the standard input device, e.g. keyboard.
The extraction operator(>>) is used along with the object cin for reading
inputs.
#include <iostream>
using namespace std;
int main( )
{
int age;
cout << “ enter your age:” ;
cin >> age;
cout << “your age is: “ <<age;
12. Standard input stream(Cin)
return 0;
}
Input:
18
Output:
Enter your age :
Your age is: 18
The above program asks the user to input the age. The object cin is connected
to the input device. The age is entered by the user is extracted from cin using
the extraction ope (>>) and this data is then stored in variable age.
13. Input/output stream example
#include<iostream.h>
Using namespace std;
int main()
{
int a, b, sum;
cout<<“enter two number:” ;
cin>>a>>b;
sum=a+b;
cout<<“sum of the two number”;
Return 0;
}
Output:
Enter two number:3 7
Sum of the two number is 10
14. Benefits of stream
Increase type safety, reduce errors, allow
extensibility, and provide inheritbility.
The input(>>) operator and output(<<) operator are
typesafe.
These operators are easier to use than scanf() and
printf().