Lecture 6: Input/Output (II) - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 6: Input/Output (II)

Description:

Write data to the standard output device. Use manipulators in ... showpoint forces output to show the decimal point and trailing zeros. 5. The setw Manipulator ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 12
Provided by: xw
Category:

less

Transcript and Presenter's Notes

Title: Lecture 6: Input/Output (II)


1
Lecture 6 Input/Output (II)
  • Introduction to Computer Science
  • Spring 2006

2
Contents
  • Write data to the standard output device
  • Use manipulators in a program to format output
  • Perform input and output operations with the
    string data type
  • File input and output

3
Writing to Standard Output
  • Syntax of cout when used with ltlt
  • coutltltexpression or manipulator
  • ltltexpression or manipulator...
  • Expression is evaluated
  • Value is printed
  • Manipulator is used to format the output

4
Formatting Output
  • endl manipulator moves output to the beginning of
    the next line
  • setprecision(n) outputs decimal numbers with up
    to n decimal places
  • fixed outputs floating-point numbers in a fixed
    decimal format
  • showpoint forces output to show the decimal point
    and trailing zeros

5
The setw Manipulator
  • setw outputs the value of an expression in
    specific columns
  • If the number of columns exceeds the number of
    columns required by the expression
  • Output of the expression is right-justified
  • Unused columns to the left are filled with spaces

6
The flush Function
  • flush clears the buffer, even if it is not full
  • Unlike endl, it does not move the cursor to the
    beginning of the next line
  • The syntax for flush
  • ostreamVar.flush()
  • ostreamVar is an output stream variable
  • flush can be used as a manipulator
  • coutltltflush

7
Additional Output Formatting Tools
  • Output stream variables can use setfill to fill
    unused columns with a character
  • left left-justifies the output
  • ostreamVarltltleft
  • Disable left by using unsetf
  • right right-justifies the output
  • ostreamVar ltlt right

8
Types of Manipulators
  • Two types of manipulators
  • With parameters
  • Without parameters
  • Parameterized require iomanip header
  • setprecision, setw, and setfill
  • Nonparameterized require iostream header
  • endl, fixed, showpoint, left, and flush

9
I/O and the string Type
  • An input stream variable (cin) and extraction
    operator gtgt can read a string into a variable of
    the data type string
  • Extraction operator
  • Skips any leading whitespace characters and
    reading stops at a whitespace character
  • Should not be used to read strings with blanks
  • The function getline
  • Reads until end of the current line
  • Should be used to read strings with blanks

10
File Input/Output
  • File area in secondary storage to hold info
  • File I/O
  • Include fstream header
  • Declare file stream variables
  • Associate the file stream variables with the
    input/output sources
  • Use the file stream variables with gtgt, ltlt, or
    other input/output functions
  • Close the filesFile Input/Output

11
End of lecture 6
  • Thank you!
Write a Comment
User Comments (0)
About PowerShow.com