22
Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University

Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University

Embed Size (px)

Citation preview

Chapter 14 Stream Input and Output

By C. Shing

ITEC Dept

Radford University

Slide 2

Objectives Understand how to use unformatted input (cin)

and output (cout) Understand how to use formatted input (cin)

and output (cout)

Slide 3

Inheritance Chart iostream class ios

istream ostream

iostream

Slide 4

Unformatted Input Use stream extraction (>>)

Input a character

char c;

cin >> c; Input a string

char string[SIZE];

cin >> string;

Slide 5

Unformatted Input (Cont.)

Example 1

Slide 6

Unformatted Input (Cont.) Use cin member function

Input a characterchar c=cin.get();

Input a linechar line[80];

cin.getline(line, 80); Input a string

char string[SIZE];cin.get(string, SIZE); or cin.read(string, SIZE);

Slide 7

Unformatted Input (Cont.)Other related cin member functions:

1. cin.eof(): returns 0 if not reach EOF. Returns 1 otherwise.

2. cin.ignore(n, c): ignore n characters until the character c

3. cin.putback(c): put the character c back to reading stream

4. cin.peek(): peek the next charcter in the reading stream

5. cin.gcount(): return # of characters last read

Slide 8

Unformatted Input (Cont.)

Example 2

Example 3

Slide 9

Unformatted Output Use stream insertion (<<)

Ouput a character

char c;

cout << c; Output a string

char string[SIZE];

cout << string;

Slide 10

Unformatted Output (Cont.) Use stream insertion (<<) (Cont.)

Ouput string address (by casting to type void *)

char string[SIZE];

cout << “string address is “

<< static_cast<void *>string; Ouput variable address

cout << “number address is “

<< &number;

Slide 11

Unformatted Output (Cont.) Use cout member function

Output a character

char c=cout.put(); Output 2 characters

char c1, c2

cout.put(c1).put(c2); Output a string

char string[SIZE];

cout.write(string, SIZE); or

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

Slide 12

Unformatted Output (Cont.)

Example 4

Slide 13

Formatted Input/Output Include stream manipulator:

#include <iomanip> Can use either parametrized stream manipulator

or member functions

Slide 14

Formatted Input

Manipulator Member function

Explain

setw(n) cin.width(n) use n characters to read

Slide 15

Formatted Input (Cont.)

Example 5

Example 6

Slide 16

Formatted Output

Manipulator Member function Explain

setbase(n)/dec/oct/hex

n=10(decimal),8(octal),16(hexadecimal)

setw(n) cout.width(n) n-1 characters to read

setprecision(n) cout.precision(n) n places after decimal point

setfill(c) cout.fill(c) Pad blank with character c

Slide 17

Formatted Output (Cont.)

Manipulator Member function Explain

setiosflags(flag)/setf(flag),resetiosflags(flag)/unsetf(flag)

cout.setf(flag), cout.unsetf(flag)

Set, reset stream format state

flags(flag)/flags() cout.flags(flag)/cout.flag()

Set format flag and return the previous format/show current format flag

Slide 18

ios Class Flags

Format state flag Explain

ios::skipws Skip white space on input stream

ios::left/ios::right Left/right justified

ios::internal Left justify sign, right justify number

ios::showbase Specify base

Slide 19

ios Class Flags (Cont.)

Format state flag Explain

ios::dec/ios::oct/ios::hex

Show values in base 10/8/16

ios::showpoint Show decimal point

ios::showpos Show signs

ios::scientific Show scientific notation

Slide 20

ios Class Flags (Cont.)

Format state flag Explain

ios::uppercase Show uppercase for letters of output

ios::fixed Show fixed floating point

ios::showpos Show signs

ios::scientific Show scientific notation

Slide 21

Formatted Output (Cont.)

Base Conversion

Slide 22

References Deitel & Deitel: C How to Program, 4th ed.,

Chapter 21, Prentice Hall