Unit-7
Managing Console
I/O Operations
I/O and File Management
Concept of streams
cin and cout objects
C++ stream classes
Unformatted and formatted I/O
Manipulators
File stream
C++ File stream classes
File management functions
File modes
Binary and random Files
Concepts of Streams
Concept of Streams
Keyboard Display / Printer
File Input Output File
Stream C++ Program Stream
Program Program
Input source Output target
to stream from stream
Concept of streams(Cont…)
A stream is a general name given to a flow of data.
A stream is a sequence of bytes.
The source stream that provides data to programs is called input
stream.
The destination stream receives output from the program is called
output stream.
In header <iostream>, a set of class is defined that supports I/O
operations.
The classes used for input/output to the devices are declared in
the IOSTREAM file.
The classes used for disk file are declared in the FSTREAM file.
Input/Output streams
Input
Input stream
device
Program
Output stream
Output
device
Stream class for console I/O operations
General input/output
input stream ios stream class
class
pointer
output stream class
istream streambuf ostream
input output
iostream input/output stream class
istream_withassign Iostream_withassign ostream_withassign
istream_withassign, ostream_withassign and iostream_withassign
ios class contains
iostream basic properties
class inherits facilities that are used ostream
of istream, by all other input
class and
through
add assignment
istream
ostream class
class operators
inherits
inherits to its base
properties
properties of
ofiosclasses.
ios
output classes(Necessary
multiple inheritance. for formatted input/output).
cout which
Extraction
Insertion is directed
operator
operator <<,
>>,to video
put()
get(),and display,
getline()
write() isare
and predefined
read()
members
are object
members
of of of
ostream
Alsoclass
The contains pointer to
ios declared asathe
buffer object(streambuf)
virtual base class so that only one copy
ostream_withassign.
istream
class class
streambuf
of provides
its members an interface
inherited to physical device through buffers.
by the iostream.
Similarly cin is an object of istream_withassign.
Unformatted and Formatted I/O
put(), get(), getline(), write() - Unformatted I/O Operations
char ch; Get a character from keyboard
cin.get(ch); Similar to cin.get(ch);
ch=cin.get(); The operator >> can also be used to read a
cin>>ch; character but it will skip the white spaces
cout.put(ch); and newline character.
cout.put('x'); put() function can be used to display value of
variable ch or character.
char name[20];
cin.getline(name,10); getline() reads whole line of text
line size that ends with newline character or
upto (size-1).
cin>>name; cin can read strings that do not contain white
spaces. write() displays string of given size, if
cout.write(name,10);
the size is greater than the length of
line, then it displays the bounds of
line.
ios Format Functions
Function Task
width() To specify the required field size for displaying an output value
precision() To specify number of digits to be displayed after the decimal point of
a float value.
fill() To specify a character that is used to fill the unused portion of a field.
setf() To specify format flags that can control the form of output.
unsetf() To clear the flags specified
Example: output:
output:
cout.precision(6
cout.fill('*');
cout.width(6); * * * 5 42 3. 6 4 5 7 5
cout.setf(ios::left,ios::adjustfield)
);
cout.width(6);
cout<<"543";
;
cout.width(10);
cout<<"543"; output:
cout.width(6);
cout<<sqrt(7); 5 4 3 # # #
cout.fill('#');
cout<<"543";
Flags and bit fields
Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
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
setf(arg1, arg2)
arg-1: one of the formatting flags.
arg-2: bit field specifies the group to which the formatting flag belongs.
Manipulators for formatted I/O operations
Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream.
To access manipulators, the file <iomanip> should be included
in the program.
Function Manipulator Meaning
width() setw() Set the field width.
precision() setprecision() Set the floating point precision.
fill() setfill() Set the fill character.
setf() setiosflags() Set the format flag.
unsetf() resetiosflags() Clear the flag specified.
“\n” endl Insert a new line and flush stream.