COM 102 Chapter 26 Stream InputOutput 1 C














- Slides: 14

COM 102 Chapter 26: Stream Input/Output 1 C How to Program Deitel & Deitel

2 Outline Streams Stream Input/Output Classes and Objects Stream Output Stream Input Stream Manipulators

3 Streams C++ Input/Output takes place in streams that are sequences of bytes. Bytes flow from device to main memory in input operation. On the other hand, in output operation bytes flow from main memory to a device. High-level formatted I/O is used. Bytes are grouped into meaningful units such as integers, floating-points, characters.

4 Stream Input/Output Classes and Objects istream cin ostream cout cerr clog

5 Stream Output of char * variables Cannot use << operator (it is used to print char * as a null terminated string We need to cast char * to a void * Address is printed as a hexadecimal number.

6 Stream Output

7 Stream Output ostream member function put Outputs a character Can be cascaded Can be called with a numeric expression that represents ASCII value cout. put(‘D’); cout. put(‘D’). put(‘n’); cout. put(68);

8 Stream Input istream member function get With no arguments: Returns one character input from the stream With a character-reference argument: Stores input character in the character-reference argument With three arguments: a character array, a size limit and a delimiter istream member function eof Returns false when end-of-file has not occured Returns true when end-of-file has occured

9 Stream Input

10 Stream Input istream member function getline Similar to the three argument version of get Only difference is delimiter is removed from the stream Three arguments: a character array, a size limit and a delimiter Reads and stores characters in the character array

11 Stream Input istream member functions peek, putback and ignore reads and discards a specified number of charactersand terminates on a specified delimiter putback places previous character obtained by a get from the input stream back into the stream peek returns the next character in the input stream but does not remove it from the stream

12 Stream Manipulators Setting field widths Setting precision Setting fill characters in fields Flushing streams Inserting a newline and flushing the output stream

13 Stream Manipulators

14 Stream Manipulators