Chapter 21 - C++ Stream Input-Output.ppt
《Chapter 21 - C++ Stream Input-Output.ppt》由会员分享,可在线阅读,更多相关《Chapter 21 - C++ Stream Input-Output.ppt(59页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 21 - C+ Stream Input/Output,Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion Operator 21.3.2 Cascading Stream-Insertion/Extraction Operators 21.3.3 Output of char * Variables
2、21.3.4 Character Output with Member Function put; Cascading puts 21.4 Stream Input 21.4.1 Stream-Extraction Operator 21.4.2 get and getline Member Functions 21.4.3 istream Member Functions peek, putback and ignore 21.4.4 Type-Safe I/O 21.5 Unformatted I/O with read, gcount and write,Outline (continu
3、ed) 21.6 Stream Manipulators 21.6.1 Integral Stream Base: dec, oct, hex and setbase 21.6.2 Floating-Point Precision (precision, setprecision) 21.6.3 Field Width (setw, width) 21.6.4 User-Defined Manipulators 21.7 Stream Format States 21.7.1 Format State Flags 21.7.2 Trailing Zeros and Decimal Points
4、 (ios:showpoint) 21.7.3 Justification (ios:left, ios:right, ios:internal) 21.7.4 Padding (fill, setfill) 21.7.5 Integral Stream Base (ios:dec, ios:oct, ios:hex, ios:showbase) 21.7.6 Floating-Point Numbers; Scientific Notation (ios:scientific, ios:fixed) 21.7.7 Uppercase/Lowercase Control (ios:upperc
5、ase) 21.7.8 Setting and Resetting the Format Flags (flags, setiosflags, resetiosflags) 21.8 Stream Error States 21.9 Tying an Output Stream to an Input Stream,Chapter 21 - C+ Stream Input/Output,21.1 Introduction,Many C+ I/O features are object-oriented Use references, function overloading and opera
6、tor overloading C+ uses type safe I/O Each I/O operation is automatically performed in a manner sensitive to the data type Extensibility Users may specify I/O of user-defined types as well as standard types,21.2 Streams,Stream A transfer of information in the form of a sequence of bytesI/O Operation
7、s: Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection),21.2 Streams (II),I/O operations are a bottleneck The time for
8、 a stream to flow is many times larger than the time it takes the CPU to process the data in the streamLow-level I/O Unformatted Individual byte unit of interest High speed, high volume, but inconvenient for people High-level I/O Formatted Bytes grouped into meaningful units: integers, characters, e
9、tc. Good for all I/O except high-volume file processing,21.2.1 Iostream Library Header Files,iostream library: : Contains cin, cout, cerr, and clog objects : Contains parameterized stream manipulators: Contains information important to user-controlled file processing operations,21.2.2 Stream Input/O
10、utput Classes and Objects,ios: istream and ostream inherit from ios iostream inherits from istream and ostream. (right-shift operator) Overloaded as stream extraction operator Both operators used with cin, cout, cerr, clog, and with user-defined stream objects,21.2.2 Stream Input/Output Classes and
11、Objects (II),istream: input streamscin someVariable; cin knows what type of data is to be assigned to someVariable (based on the type of someVariable). ostream: output streams cout someVariable; cout knows the type of data to output cerr someString; Unbuffered - prints someString immediately. clog s
12、omeString; Buffered - prints someString as soon as output buffer is full or flushed,21.3 Stream Output,ostream: performs formatted and unformatted output Uses put for characters and write for unformatted characters Output of numbers in decimal, octal and hexadecimal Varying precision for floating po
13、ints Formatted text outputs,21.3.1 Stream-Insertion Operator, is overloaded to output built-in types Can also be used to output user-defined types cout n; Prints newline character cout endl; endl is a stream manipulator that issues a newline character and flushes the output buffer cout flush; flush
14、flushes the output buffer,21.3.2 Cascading Stream-Insertion/Extraction Operators, : Associates from left to right, and returns a reference to its left-operand object (i.e. cout). This enables cascading cout “How“ “ are“ “ you?“;Make sure to use parenthesis:cout “1 + 2 = “ (1 + 2); NOT cout “1 + 2 =
15、“ 1 + 2;,21.3.3 Output of char * Variables, will output a variable of type char * as a stringTo output the address of the first character of that string, cast the variable as type void *,1. Initialize string2. Print string2.1 cast into void *2.2 Print value of pointer (address of string)Program Outp
16、ut,Value of string is: test Value of static_cast( string ) is: 0046C070,21.3.4 Character Output with Member Function put; Cascading puts,put member function Outputs one character to specified streamcout.put( A); Returns a reference to the object that called it, so may be cascadedcout.put( A ).put( n
17、 ); May be called with an ASCII-valued expressioncout.put( 65 ); Outputs A,21.4 Stream Input, (stream-extraction) Used to perform stream input Normally ignores whitespaces (spaces, tabs, newlines) Returns zero (false) when EOF is encountered, otherwise returns reference to the object from which it w
18、as invoked (i.e. cin) This enables cascaded input cin x y; controls the state bits of the stream failbit set if wrong type of data input badbit set if the operation fails,21.4.1 Stream-Extraction Operator, and grade) Extraction returns 0 (false) when EOF encountered, and loop ends,1. Initialize vari
19、ables2. Perform loop3. OutputProgram Output,Enter grade (enter end-of-file to end): 67 Enter grade (enter end-of-file to end): 87 Enter grade (enter end-of-file to end): 73 Enter grade (enter end-of-file to end): 95 Enter grade (enter end-of-file to end): 34 Enter grade (enter end-of-file to end): 9
20、9 Enter grade (enter end-of-file to end): Z Highest grade is: 99,21.4.2 get and getline Member Functions,cin.get(): inputs a character from stream (even white spaces) and returns itcin.get( c ): inputs a character from stream and stores it in c,21.4.2 get and getline Member Functions (II),cin.get(ar
21、ray, size): Accepts 3 arguments: array of characters, the size limit, and a delimiter ( default of n). Uses the array as a buffer When the delimiter is encountered, it remains in the input stream Null character is inserted in the array Unless delimiter flushed from stream, it will stay therecin.getl
22、ine(array, size) Operates like cin.get(buffer, size) but it discards the delimiter from the stream and does not store it in array Null character inserted into array,1. Initialize variables2. Input data2.1 Function call3. OutputProgram Output,Before input, cin.eof() is 0 Enter a sentence followed by
23、end-of-file: Testing the get and put member functionsZ Testing the get and put member functions EOF in this system is: -1 After input cin.eof() is 1,1. Initialize variables2. Input2.1 Function call3. OutputProgram Output,Enter a sentence: Using the getline member functionThe sentence entered is: Usi
24、ng the getline member function,21.4.3 istream Member Functions peek, putback and ignore,ignore member function Skips over a designated number of characters (default of one) Terminates upon encountering a designated delimiter (default is EOF, skips to the end of the file) putback member function Plac
25、es the previous character obtained by get back in to the stream. peek Returns the next character from the stream without removing it,21.4.4 Type-Safe I/O, operators Overloaded to accept data of different types When unexpected data encountered, error flags set Program stays in control,21.5 Unformatte
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER21CSTREAMINPUTOUTPUTPPT
