C++ Input/Output : Streams
This blog has been migrated to learn c programming for beginners
The input/output operations which involve keyboard as input device and screen as output device are known as Console I/O operations . The input/output operations which involve file as input device and output device are known as File I/O operations
C++ Input/Output implement concept of stream .It is an object-oriented alternative to C's FILE-based streams from the C standard library.Stream is the sequences of bytes or flow of data , which acts as a source from which the input data can be obtained by a program or a destination to which the output data can be sent by the program.
Input Stream :
It is flow of data bytes from a device (e.g Keyboard , disk drive) to main memory
Output Stream :
It is flow of data bytes from main memory (i.e program) to a device
Stream Class Hierarchy
Stream class is the collection of data and the methods necessary to control and maintain flow of data or stream .These stream class are declared in the header file iostream.h . The different stream class available in C++ are defined in following hierarchical order.
Unformatted I/O operations
Unformatted I/O operation do not allow user to read or display data in desired format. The unformatted operations supported by C++ are :
Overloaded operator << and >>
The insertion operator (<<) and extraction operator (>>) are overloaded operations which are used for reading and writing data to/from input/output. The predefined object cout is an object of ostream class. The ostream is said to be connected with standard output devices (i.e screen).The cout is used in conjunction with insertion (<<) operator . The C++ compiler also determines the data type of variable to be output ans selects the appropriate stream insertion operator to display data
Syntax
cout<<"message"<varialble;
The predefined object cin is an object of istream class. The istream is said to be connected with standard input devices (i.e keyboard).The cin is used in conjunction with extraction(>>) operator . The C++ compiler also determines the data type of input data ans selects the appropriate stream extraction operator to read data
Syntaxcin>>varialble;
put() and get() functions
The class istream defines get() and ostream defines put() to read and display single character
Syntaxcin.get(character_variable);
cin.put(character_variable);
getline() and write() function
The getline reads the whole line untile the enter key is encountred and write() display the whole line
Syntaxcin.getline(string_variable , size);
cout.write(string , size);
Formatted Console I/O Operations:
Member Functions of ios class and flags :
Member Function | Function | Syntax |
---|---|---|
width() | The default width of your output will be just enough space to print the number, character, or string in the output buffer. You can change this by using width(). Because width() is a member function, it must be invoked with a cout object. It only changes the width of the very next output field and then immediately reverts to the default. | cout.width(w); |
Precision() | It used to set the number of integers displayed after a decimal point. | cout.precision(w); |
setf() | This is another function of ios class which is used to set various flags used for formatting text in C++ .The function takes two arguments as follows:cout.setf(argument1 ,argument2) where argument1 is one of the formatting flags defined in the class ios , which is used to specify action on the output and argument2 is known as bit field that specifies the group to which the formatting flags belongs | |
unsetf() | This is another function of ios class which is used to unset various flags used for formatting text in C++ .The function takes two arguments as follows:cout.unsetf(argument1) |
List of ios flags :
Description of format | Flags | Bit field |
---|---|---|
Left justified output | ios::left | ios:adjusted |
Right justified output | ios::right | ios:adjusted |
Padding after sign | ios::intrenal | ios:adjusted |
Scientific notation | ios::scientific | ios:floatfield |
Fixed point notation | ios::fixed | ios:floatfield |
Decimal base | ios::dec | ios:basefield |
Octal base | ios::oct | ios:basefield |
Hexadecimal base | ios::hex | ios:basefield |
Comments
Post a Comment