lecture contents i/o streams. –input/output streams. –unformatted vs formatted streams....

29
Lecture Contents I/O Streams. Input/output streams. Unformatted vs formatted streams. Stream manipulators. Stream error state. Stream tying. Examples.

Upload: janis-andrews

Post on 13-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Lecture Contents

• I/O Streams.– Input/output streams.– Unformatted vs formatted streams.– Stream manipulators.– Stream error state.– Stream tying.– Examples.

I/O Streams

• C++ uses type-safe I/O, each I/O operation is sensitive to data type.

• The most basic functions for I/O are in <iostream>.

• They are abstracted as stream of data.

I/O Streams

• Streams are sequences of bytes.• In input operations, the bytes flow from a device

to main memory. • In output operations bytes flow from main memory

to a device. • Formatted I/O vs. unformatted I/O.

I/O Streams

Basic data type for formatted stream.

• char - for classical I/O stream. Each character is 1 byte.

• wchar_t for modern I/O stream which takes into account UNICODE.

• Library functions for formatted streams are in <iomanip>.

I/O Streams

Standard I/O templates:

Overloading operator for output streams: >>

Overloading operator for input streams: <<

I/O Streams

Standard I/O Classes:

I/O Streams

ios class:

I/O Streams

Standard Stream Objects:• cin (istream): use extraction operator >>.• cout (ostream): use insertion operator <<;• cerr (ostream): use to connect with standard

error output device. (unbuffered).• clog (ostream): similar to cerr but use buffer.

buffered vs unbuferred output is whether the output will be delayed. (endl could be used to flush buffers).

I/O Streams

Stream Output:

- cout use insertion operator <<.

- cout.put()

cout.put( 'A' );

cout.put( 'A' ).put( '\n' );

cout.put( 65 );

I/O Streams

Stream Input: using cin object and extraction operator >>, usually skip blank characters.

• cin.eof() - return true if encounter a end-of-file character in the stream (Ctrl-Z on Windows, Ctrl-D on Unix/Linux).

• cin.get() - read next character from the buffer.

- read until reaching a delimiter.

- read until reaching a maximal number

of characters

I/O Streams

Stream Input: - Examples

I/O Streams

Stream Input: - Examples

I/O Streams

Stream Input: cin.getline() similar to cin.get() (version 3) but insert NULL character at the end and peeked the delimiter character out of stream.

I/O Streams

Stream Unformatted I/O:

- Unformatted I/O is read/write raw bytes directly from/to input/output streams it is used for low level processing of data (where we do not care about data types).

cout.write() - write raw bytes to the output stream.

cout.read() - read raw bytes from the input stream.

cin.gcount() - return the number of characters read from the last input.

I/O Streams

Stream Unformatted I/O: - Example

I/O Streams

Stream Manipulators: - <iomanip>

I/O Streams

Stream Manipulators: - <iomanip>

I/O Streams

Stream Manipulators:

Integral stream base: - formatting integers (oct, dec, hex, setbase).

int number;

…..

cout<< std::dect<< number<<" "<<std::oct<<number;

cout<<std::setbase(16)<<number;

I/O Streams

Stream Manipulators: - Floating-point - precision.

<iomanip>

I/O Streams

Stream Manipulators: - Set Field Width (width, setw).

I/O Streams

Stream Manipulators: - User's defined streams

I/O Streams

Stream Manipulators: - User's defined streams

I/O Streams

Stream Manipulators: - Justification (left, right)

I/O Streams

Stream Manipulators: - Padding (setfill)

I/O Streams

Stream Manipulators: - Formatting characters

I/O Streams

Stream Error States:

I/O Streams

Tying an Output stream to an Input stream:

C++ allow to tie an output stream to an input stream to make sure that the output will appear before the corresponding input.

cin.tie(&cout); //default by C++

To untie:

cin.tie(0);

I/O Streams

More Example:

I/O Streams

Further Reading:

1. Textbook 1 Chapter 15.

2. Workbook 1 Chapter 12.