Streams

Streams:

C/C++ IO are based on streams concept, which are sequence of bytes flowing in and out of the programs . In input operations, data bytes flows from an input source like keyboard, file, network or another program, into the program. In output operations, data bytes flows from the program to an output sink such as console, file, network or another program. Streams acts as intermediaries between the programs and the actual IO devices, in such the way that free the programmers from handling the actual devices, in order to archive device independent IO operations.
To perform output and input, a C++ program:
1. Construct a stream object.
2. Connect (Associate) the stream object to an actual IO device like keyboard, console, file, network, another program.
3. Perform input/output operations on the stream, via functions define in the stream’s pubic interface in a device independent manner. Some functions convert the data between the external format & internal format (formatted IO); while other does not (unformatted or binary IO).
4. Disconnect the stream to the actual IO device that is close the file.
5. Free the stream object.

Example 1

#include<iostream.h>

void main(void)

{

cout<<“Welcome to C++ I/O module!!!”<<endl;

cout<<“Welcome to “;

cout<<“C++ oop programming “<<endl;

}

Output
Welcome to C++ I/O module!!!

Welcome to C++ oop programming

Example 2:

#include<iostream.h>

void main(void)

{

int p2 = 3, q2 = 10;

cout << “Concatenating program using << operator.\n”;

cout << “70 minus 20 is “<<(70 – 20)<<endl;

cout << “55 plus 4 is “<<(55 + 4)<<endl;

cout <<p2<<” + “<<q2<<” = “<<(p2+q2)<<endl;

}

Output
Concatenating program using << operator.

70 minus 20 is 50

55 plus 4 is 59

3 +10=13

Example 3

#include<iostream.h>

void main(void)

{

int p1, q1, r1; cout << “Enter 3 integers separated by space: \n”;

cin>>p1>>q2>>r1;

cout<<“Sum of the “<<p1<<“,”<<q2<<” and “<<r1<<” is = “<<(p1+q1+r1)<<endl;

}

Output:
Enter 3 integers separated by space:7   9   6

Sum of the 7,9and 6=22

 

Leave a Reply

Your email address will not be published. Required fields are marked *