Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples."— Presentation transcript:

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

2 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. They are abstracted as stream of data.

3 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.

4 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.

5 I/O Streams Standard I/O templates: Overloading operator for output streams: >> Overloading operator for input streams: <<

6 I/O Streams Standard I/O Classes:

7 I/O Streams ios class:

8 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).

9 I/O Streams Stream Output: - cout use insertion operator <<. - cout.put() cout.put( 'A' ); cout.put( 'A' ).put( '\n' ); cout.put( 65 );

10 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

11 I/O Streams Stream Input: - Examples

12 I/O Streams Stream Input: - Examples

13 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.

14 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.

15 I/O Streams Stream Unformatted I/O: - Example

16 I/O Streams Stream Manipulators : -

17 I/O Streams Stream Manipulators : -

18 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;

19 I/O Streams Stream Manipulators : - Floating-point - precision.

20 I/O Streams Stream Manipulators : - Set Field Width (width, setw).

21 I/O Streams Stream Manipulators : - User's defined streams

22 I/O Streams Stream Manipulators : - User's defined streams

23 I/O Streams Stream Manipulators : - Justification (left, right)

24 I/O Streams Stream Manipulators : - Padding (setfill)

25 I/O Streams Stream Manipulators : - Formatting characters

26 I/O Streams Stream Error States:

27 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);

28 I/O Streams More Example:

29 I/O Streams Further Reading: 1. Textbook 1 Chapter 15. 2. Workbook 1 Chapter 12.


Download ppt "Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples."

Similar presentations


Ads by Google