- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Input Output Stream
In C++, all devices are treated as files. Thus, the standard input device (the keyboard), the standard output device (the screen or monitor), and the standard error device (the screen or monitor) (where the errors, if any, are displayed) are all treated as files. At its lowest level, a file is interpreted simply as a sequence, or stream of bytes. At this level, the notion of a data type is absent i.e., data is treated simply as sequence of bytes without considering its data type. However, at the user level, a file consists of a sequence of possibly intermixed data types-characters, arithmetic values, class objects etc.
Function of iostream.h
There are the following functions of iostream.h at different level :
- At the lowest implementation level, where the notion of data type is missing and files are treated as streams of bytes, the I/O library manages the transfer of these bytes.
- At the user level, where the notion of data types is present, I/O library manages the interface between these two levels i.e., between user level and the lowest implementation level.
- The I/O library predefines a set of operations for handling reading and writing of build-in data types.
Predefined Streams in I/O Library
As already mentioned, at lowest levels, files are implemented as streams of bytes. A Stream is simply a sequence of bytes. Input and Output operations are supported by the istream (input stream) and ostream (output stream) classes.
The ostream output operation, referred to as insertion is performed by the insertion operator "<<". The istream input operation, referred to as extraction, is performed by the extraction operator ">>".
Predefined Stream Objects
The predefined stream objects for the input, output and the error are as follows :
- cin (pronounced "see-in"), as istream class object tied to standard input. cin stands for console input.
- cout (pronounced "see-out"), as ostream class object tied to standard output. cout stands for console output.
- cerr (pronounced "see-err"), as ostream class object tied to standard error. cerr stands for console error.
Let's discuss the examples of all the above three predefined stream objects for input, output and error :
C++ standard output stream
Here is an example on standard output stream in C++
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char str[] = "Welcome to codescracker.com\nThis is C++ Tutorial."; cout<<"Value of str is:\n"; cout<<str; getch(); }
Here is the sample output of this C++ program:

C++ standard input stream
Here is an example uses standard input stream in C++:
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char name[20]; cout<<"Enter your name: "; cin>>name; cout<<"Your name is "<<name; getch(); }
Here is the sample run of the above C++ program:

C++ standard error stream
Here is an example helps you in understanding the standard error stream in C++:
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char str[]="Unable to read...."; cerr<<"Error message: "<<str; getch(); }
Here is the sample run of this C++ program:

Now, let's discuss about the standard log stream i.e. clog :
C++ standard log stream
Here is an example uses standard log stream in C++:
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char str[]="Unable to read..."; clog<<"Error message: "<<str; getch(); }
C++ Input Output Stream Example
Here are some C++ input output stream example programs, demonstrating the uses of input and output streams in C++
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int num; char ch; char str[80]; float f; cout<<"Enter a number: "; cin>>num; cout<<"\nYou entered "<<num; cout<<"\n\nEnter a character: "; cin>>ch; cout<<"\nYou entered "<<ch; cout<<"\n\nEnter a floating-point value: "; cin>>f; cout<<"\nYou entered "<<f; cout<<"\n\nEnter your name: "; cin>>str; cout<<"\nYou entered "<<str; cout<<"\n\nPress any key to exit...\n"; getch(); }
Below is the sample run of the above C++ program:

Let's take another C++ program:
/* C++ Input and Output Stream */ #include<iostream.h> #include<conio.h> void main() { clrscr(); char sub1[20], sub2[20]; float mark1, mark2; float sum=0, avg=0; cout<<"Enter two subject's name: "; cin>>sub1>>sub2; cout<<"\nEnter marks obtained in these two subjects: "; cin>>mark1>>mark2; sum=mark1+mark2; avg=sum/2; cout<<"\nYou got total "<<sum<<" marks with an average of "<<avg; cout<<"\nin two subjects namely "<<sub1<<" and "<<sub2; getch(); }
Here is the sample run of the above C++ program:

More Examples
Here are some more C++ programs listed, that you can go for:
- Add two Numbers
- Print Table of Number
- Print Prime Numbers
- Add n Numbers
- Interchange Numbers
- Reverse Numbers
- Swap two Numbers
« Previous Tutorial Next Tutorial »