Basic Input/Output in C++





  • C++'s Input and Output is based on streams.
  • Stream is a sequence of bytes that flows into and out of our program.
  • Input Stream : In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program.
  • Output Stream : In output operations, data bytes flow from the program to an output sink (such as console, file, network or another program).
  • Streams acts as an intermediaries between the programs and the actual IO devices.
  • it's an abstract representation of Input/Output Stream..
    prowessapps.in
  • The iostream library is an object-oriented library that provides input and output functionality using streams in C++.
  • If stream's bytes flow from input source/device to main memory, this is called input operation through Input Stream (istream).
  • If stream's bytes flow from main memory to media/device, this is called output operation through Output Stream (ostream).
  • Stram based I/O operations are done by using library header file :
    #include <iostream>
    OR
    #include <iostream.h>

The iostream in C++:

When you include the iostream header, you gain access to a whole hierarchy of classes responsible for providing I/O functionality (including one class that is actually named iostream). The class hierarchy for the non-file-I/O classes looks like this:

prowessapps.in

Input/Output in C++:

  • The istream class is the primary class used when dealing with input streams. With input streams, the extraction operator (>>) is used to remove values from the stream.
  • The ostream class is the primary class used when dealing with output streams. With output streams, the insertion operator (<<) is used to put values in the stream.
  • The iostream class can handle both input and output, allowing bidirectional I/O.

Standard Streams in C++:

C++ comes with four predefined standard stream objects that have already been set up for your use.

The Standard Output Stream ( cout ):

The predefined object cout is an instance of ostream class.
The cout is used to print text on Terminal/Monitor.

Example:

#include<iostream>
using namespace std;
int main( ) {
 char str[] = "C++ Programming";
 cout<< "Value of str is : "<< str;
 cout<< endl;
 return 0;
}
OUTPUT:
Value of str is : C++ Programming

The insertion operator << may be used more than once in a single statement as shown above and endl is used to add a new-line at the end of the line.



The Standard Input Stream ( cin ):

The predefined object cin is an instance of itream class.
The cin is used to take input.
The cin object is said to be attached to the standard input device, which usually is the keyboard.

Example:

#include<iostream>
using namespace std;
int main( ) {
 char str[50];
 cout<< "Enter a string : ";
 cin>>str;
 cout<< "Entered string is : "<< str;
 cout<< endl;
 return 0;
}
OUTPUT:
Enter a string : Hello
Entered string is : Hello
The stream extraction operator >> may be used more than once in a single statement.
cin>>str >> age >> price;

  This is equivalent to

cin>>str;
cin>>age;
cin>>price;


The Standard Error Stream ( cerr ):

The predefined object cerr is an instance of ostream class.
The cerr is used to print error text on Terminal/Monitor.
But the object cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.

Example:

#include<iostream>
using namespace std;
int main( ) {
 cerr<< "Error Occured";
 return 0;
}
OUTPUT:
Error Occured


The Standard Log Stream ( clog ):

The predefined object clog is an instance of ostream class.
The clog object is said to be attached to the standard error device, which is also a display screen .
But the object clog is buffered.
Example:
#include<iostream>
using namespace std;
int main( ) {
 clog<< "Error Occured";
 return 0;
}
OUTPUT:
Error Occured

NOTE: From the above examples, You would not be able to see any difference in cout, cerr and clog, but this is good practice to display error messages using cerr stream and while displaying other log messages then clog should be used.


Next topic is Basic Input/Output in C++





 






Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved