Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University."— Presentation transcript:

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

2 Slide 2 Objectives Understand how to use unformatted input (cin) and output (cout) Understand how to use formatted input (cin) and output (cout)

3 Slide 3 Inheritance Chart iostream class ios istream iostream ostream

4 Slide 4 Unformatted Input Use stream extraction (>>) Input a character char c; cin >> c; Input a string char string[SIZE]; cin >> string;

5 Slide 5 Unformatted Input (Cont.) Example 1

6 Slide 6 Unformatted Input (Cont.) Use cin member function Input a character char c=cin.get(); Input a line char line[80]; cin.getline(line, 80); Input a string char string[SIZE]; cin.get(string, SIZE); or cin.read(string, SIZE);

7 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

8 Slide 8 Unformatted Input (Cont.) Example 2 Example 3

9 Slide 9 Unformatted Output Use stream insertion (<<) Ouput a character char c; cout << c; Output a string char string[SIZE]; cout << string;

10 Slide 10 Unformatted Output (Cont.) Use stream insertion (<<) (Cont.) Ouput string address (by casting to type void *) char string[SIZE]; cout << “string address is “ string; Ouput variable address cout << “number address is “ << &number;

11 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());

12 Slide 12 Unformatted Output (Cont.) Example 4

13 Slide 13 Formatted Input/Output Include stream manipulator: #include Can use either parametrized stream manipulator or member functions

14 Slide 14 Formatted Input ManipulatorMember function Explain setw(n)cin.width(n)use n characters to read

15 Slide 15 Formatted Input (Cont.) Example 5 Example 6

16 Slide 16 Formatted Output ManipulatorMember functionExplain setbase(n)/dec/oct /hex n=10(decimal),8( octal),16(hexadeci mal) 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

17 Slide 17 Formatted Output (Cont.) ManipulatorMember functionExplain setiosflags(flag)/s etf(flag),resetiosfl ags(flag)/unsetf(fl ag) cout.setf(flag), cout.unsetf(flag) Set, reset stream format state flags(flag)/flags()cout.flags(flag)/co ut.flag() Set format flag and return the previous format/show current format flag

18 Slide 18 ios Class Flags Format state flagExplain ios::skipwsSkip white space on input stream ios::left/ios::rightLeft/right justified ios::internalLeft justify sign, right justify number ios::showbaseSpecify base

19 Slide 19 ios Class Flags (Cont.) Format state flagExplain ios::dec/ios::oct/io s::hex Show values in base 10/8/16 ios::showpointShow decimal point ios::showposShow signs ios::scientificShow scientific notation

20 Slide 20 ios Class Flags (Cont.) Format state flagExplain ios::uppercaseShow uppercase for letters of output ios::fixedShow fixed floating point ios::showposShow signs ios::scientificShow scientific notation

21 Slide 21 Formatted Output (Cont.) Base Conversion

22 Slide 22 References Deitel & Deitel: C How to Program, 4th ed., Chapter 21, Prentice Hall


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

Similar presentations


Ads by Google