My Training Period: xx hours

The C++ formatted I/O
programming skills:

To understand and use various member functions for
C++ formatted I/O.

To understand and use various stream manipulators
for C++ formatted I/O.

18.1 iostream
Library

In
C Formatted I/O you have learned the formatted I/O in C by calling
various standard functions. In this Module we will discuss
how this formatted I/O implemented in C++ by using member functions
and stream manipulators.

We will only discuss the formatted I/O
here, for file I/O and some of the member functions mentioned in
this Module, will be presented in another Module. The discussion
here will be straight to the point because some of the terms used
in this Module have been discussed extensively in
C Formatted I/O.

Object of the ostream
class connected to standard error device. This is unbuffered
output, so each insertion to cerr
causes its output to appear immediately.

clog

Same as cerr
but outputs to clog
are buffered.

Table 18.3: iostream
operators

For
file processing C++ uses (will be discussed in another Module) the following
classes:

Class

Brief description

ifstream

To perform file input operations.

ofstream

For file output operation.

fstream

For file input/output operations.

Table 18.4: File input/output
classes

Stream
output program example:

// string output
using <<

#include
<iostream>

using namespace std;

void main(void)

{

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

cout<<"Welcome
to ";

cout<<"C++
module 18"<<endl;

//
endl is end line stream manipulator

//
issue a new line character and flushes the output buffer

//
output buffer may be flushed by
cout<<flush;
command

}

Output:

//
concatenating <<

#include
<iostream>

using namespace std;

void main(void)

{

int p
= 3, q = 10;

cout <<
"Concatenating using << operator.\n"

<<"--------------------------------"<<endl;

cout <<
"70 minus 20 is "<<(70 - 20)<<endl;

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

cout <<p<<"
+ "<<q<<" = "<<(p+q)<<endl;

}

Output:

Stream
input program example:

#include
<iostream.h>

using namespace std;

void main(void)

{

int p,
q, r;

cout <<
"Enter 3 integers separated by space: \n";

cin>>p>>q>>r;

//
the >> operator skips whitespace characters such as tabs,

//
blank space and newline. When eof is encountered, zero (false) is returned.

cout<<"Sum
of the "<<p<<","<<q<<" and "<<r<<" is = "<<(p+q+r)<<endl;

}

Output:

18.3 get()and getline()
Member Functions
of Stream Input

For the get()
function, we have three versions.

get()
without any arguments, input one character from the designated
streams including whitespace and returns this character as the
value of the function call. It will return
EOF when end of file on the stream
is encountered. For example:

cin.get();

get()
with a character argument, inputs the next character from the
input stream including whitespace. It return false when
end of file is encountered while returns a reference to the
istream object for which the get member function is being invoked.
For example:

char ch;

...

cin.get(ch);

get()
with three arguments, a character array, a size limit and a
delimiter (default value ‘\n’).
It reads characters from the input stream, up to one less than
the specified maximum number of characters and terminates or
terminates as soon as the delimiter is read. For example:

char
namevar[30];

...

cin.get(namevar,
30);

// get
up to 29 characters and inserts null

// at
the end of the string stored in variable

// namevar.
If a delimiter is found,

// the
read terminates. The delimiter

// is
left in the stream, not stored in the array.

getline() operates
like the third get() and insert a null
character after the line in the character array. It removes the
delimiter from the stream, but does not store it in the character array.

A program
examples:

//
end of file controls depend on system

//
Ctrl-z followed by return key - IBM PC, Ctrl-d - UNIX and MAC

#include
<iostream>

using namespace std;

void main(void)

{

char p;

cout <<"Using
member functions get(), eof() and put()\n"

<<"---------------------------------------------"<<endl;

cout<<"Before
any input, cin.eof() is "<<cin.eof()<<endl;

cout<<"Enter
a line of texts followed by end of file control: "<<endl;

while((p
= cin.get()) !=EOF)

cout.put(p);

cout<<"\nAfter
some text input, cin.eof() is "<<cin.eof()<<endl;

}

Output:

//
another get() version

#include
<iostream>

using namespace std;

const
int SIZE = 100;

void main(void)

{

char bufferOne[SIZE],
bufferTwo[SIZE];

cout <<"Enter
a line of text:"<<endl;

cin>>bufferOne;

//
store the string in array bufferOne

//
just the first word in the array string, then the

//
first whitespace encountered

cout<<"\nThe
line of text read by cin>> was:"<<endl;

cout<<bufferOne<<endl;

cin.get(bufferTwo,
SIZE);

//
the rest of the string

cout<<"The
line of text read by cin.get(bufferTwo,SIZE) was:"<<endl;

cout<<bufferTwo<<endl;

}

Output:

//
getline() example

#include
<iostream>

using namespace std;

const
SIZE = 100;

void main(void)

{

char buffer[SIZE];

cout<<"Read
by cin.getline(buffer, SIZE)\n"

<<"--------------------------------\n"<<"Enter a line of text:"<<endl;

cin.getline(buffer,
SIZE);

cout<<"The
line of text entered is: "<<endl;

cout<<buffer<<endl;

}

Output:

ignore() member function skips over a designated
number of characters (default is one character) or terminates upon encountering
a designated delimiter (default is EOF).
For example:

cin.ignore();
//
gets and discards 1 character.

cin.ignore(5);
//
gets and discards 5 characters.

cin.ignore(20,’\n’);

// gets
and discards up to 20 characters or until

// newline
character whichever comes first.

putback() member function places the previous
character obtained by a get() on an input stream
back onto that stream. For example:

char
chs;

...

cin.putback(chs);

// put
character back in the stream

peek() member function returns the next character
from an input stream, but does not remove the character from the stream.
For example:

char
chs;

...

chs
= cin.peek();

// peek
at character

Unformatted
I/O performed with read() and
write() member functions. They simply
input or output as raw byte.

The
read() member function extracts a given number
of characters into an array and the write()
member function inserts n characters (nulls
included). For example:

char
texts[100];

...

cin.read(texts,
100);

// read
100 characters from input stream and don’t append ‘\0’

Program
example:

//
using read(), write() and gcount() member functions

#include
<iostream>

using namespace std;

const
int SIZE = 100;

void main(void)

{

char buffer[SIZE];

cout<<"Enter
a line of text:"<<endl;

cin.read(buffer,45);

cout<<"The
line of text entered was: "<<endl;

cout.write(buffer,
cin.gcount());

//
the gcount() member function returns

//
the number of unformatted characters last extracted

cout<<endl;

}

Output:

18.4 Stream Manipulators

Used
to perform formatting, such as:

Setting field width.

Precision.

Unsetting format flags.

Flushing stream.

Inserting newline in the output stream and
flushing the stream.

Inserting the null character in the output
stream.

Skipping whitespace.

Setting the fill character in field.

18.4.1 Stream Base

For
stream base we have:

Operator/function

Brief description

hex

To set the base to hexadecimal, base
16.

oct

To set the base to octal, base
8.

dec

To reset the stream to decimal.

setbase()

Changing the base of the stream, taking
one integer argument of 10,
8 or
16
for decimal, base 8
or base 16
respectively.
setbase() is parameterized stream manipulator
by taking argument, we have to include iomanipheader file.

Table 18.5: Stream base operator
and function.

Program
example:

//
using hex, oct, dec and setbase stream manipulator

#include
<iostream>

using namespace std;

#include <iomanip>

void main(void)

{

int p;

cout<<"Enter
a decimal number:"<<endl;

cin>>p;

cout<<p<<
" in hexadecimal is: "

<<hex<<p<<'\n'

<<dec<<p<<"
in octal is: "

<<oct<<p<<'\n'

<<setbase(10)
<<p<<" in decimal is: "

<<p<<endl;

cout<<endl;

}

Output:

18.4.2 Floating-point Precision

Used
to control the number of digits to the right of the decimal point.

Use
setprecision() or precision().

precision
0 restores to the default precision of
6 decimal points.

//
using precision and setprecision

#include
<iostream>

using namespace std;

#include <iomanip>

// using C++
wrappers to access C function

#include <cmath>

void main(void)

{

double
theroot = sqrt(11.55);

cout<<"Square
root of 11.55 with various"<<endl;

cout<<"
precisions"<<endl;

cout<<"---------------------------------"<<endl;

cout<<"Using
'precision':"<<endl;

for(int
poinplace=0; poinplace<=8; poinplace++)

{

cout.precision(poinplace);

cout<<theroot<<endl;

}

cout<<"\nUsing
'setprecision':"<<endl;

for(int
poinplace=0; poinplace<=8; poinplace++)

cout<<setprecision(poinplace)<<theroot<<endl;

}

Output:

18.4.3 Field Width

Sets
the field width and returns the previous width. If values processed
are smaller than the field width, fill characters are inserted as padding.
Wider values will not be truncated.